using IWshRuntimeLibrary;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace BeDesignerSCADA.Helper
{
///
/// 系统操作类
///
public class SystemHelperNew
{
private volatile static SystemHelperNew _Instance;
public static SystemHelperNew GetInstance => _Instance ?? (_Instance = new SystemHelperNew());
private SystemHelperNew() { }
///
/// 获取当前应用程序名称,包括后缀名
///
public string GetApplicationName => $"{AppDomain.CurrentDomain.FriendlyName}.exe";
///
/// 获取当前应用程序完整路径
///
public string GetApplicationPath => $"{AppDomain.CurrentDomain.BaseDirectory}{GetApplicationName}";
///
/// 创建桌面快捷方式
///
/// 成功或失败
public void CreateShortcutOnDesktop()
{
//添加引用 (com->Windows Script Host Object Model),using IWshRuntimeLibrary;
String shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), $"{AppDomain.CurrentDomain.FriendlyName}.lnk");
if (!System.IO.File.Exists(shortcutPath))
{
// 获取当前应用程序目录地址
String exePath = Process.GetCurrentProcess().MainModule.FileName;
IWshShell shell = new WshShell();
// 确定是否已经创建的快捷键被改名了
foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk"))
{
WshShortcut tempShortcut = (WshShortcut)shell.CreateShortcut(item);
if (tempShortcut.TargetPath == exePath)
{
return;
}
}
WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath);
shortcut.TargetPath = exePath;
shortcut.Arguments = "";// 参数
shortcut.Description = AppDomain.CurrentDomain.FriendlyName;
shortcut.WorkingDirectory = Environment.CurrentDirectory;//程序所在文件夹,在快捷方式图标点击右键可以看到此属性
shortcut.IconLocation = exePath;//图标,该图标是应用程序的资源文件
//shortcut.Hotkey = "CTRL+SHIFT+W";//热键,发现没作用,大概需要注册一下
shortcut.WindowStyle = 1;
shortcut.Save();
}
}
public void CreateShortcutOnDesktop(string Name)
{
//添加引用 (com->Windows Script Host Object Model),using IWshRuntimeLibrary;
String shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), $"{Name}.lnk");
if (!System.IO.File.Exists(shortcutPath))
{
// 获取当前应用程序目录地址
String exePath = $"{AppDomain.CurrentDomain.BaseDirectory}{Name}.exe";// Process.GetCurrentProcess().MainModule.FileName;
IWshShell shell = new WshShell();
// 确定是否已经创建的快捷键被改名了
foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk"))
{
WshShortcut tempShortcut = (WshShortcut)shell.CreateShortcut(item);
if (tempShortcut.TargetPath == exePath)
{
return;
}
}
WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath);
shortcut.TargetPath = exePath;
shortcut.Arguments = "";// 参数
shortcut.Description = AppDomain.CurrentDomain.FriendlyName;
shortcut.WorkingDirectory = Environment.CurrentDirectory;//程序所在文件夹,在快捷方式图标点击右键可以看到此属性
shortcut.IconLocation = exePath;//图标,该图标是应用程序的资源文件
//shortcut.Hotkey = "CTRL+SHIFT+W";//热键,发现没作用,大概需要注册一下
shortcut.WindowStyle = 1;
shortcut.Save();
}
}
///
/// 设置开机自启动
///
/// true:开机启动,false:不开机自启
public void AutoStart(bool isAuto = true)
{
if (isAuto == true)
{
RegistryKey R_local = Registry.CurrentUser;
RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
R_run.SetValue(GetApplicationName, GetApplicationPath);
R_run.Close();
R_local.Close();
}
else
{
RegistryKey R_local = Registry.CurrentUser;
RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
R_run.DeleteValue(GetApplicationName, false);
R_run.Close();
R_local.Close();
}
}
///
/// 判断是否是自动启动
///
///
public bool IsAutoStart()
{
RegistryKey R_local = Registry.CurrentUser;
RegistryKey R_run = R_local.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
bool res = R_run.GetValueNames().Contains(GetApplicationName);
R_run.Close();
R_local.Close();
return res;
}
#region U盘,串口插拔信息
private const int WM_DEVICECHANGE = 0x219; //设备改变
private const int DBT_DEVICEARRIVAL = 0x8000; //检测到新设备
private const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //移除设备
public const int DBT_DEVTYP_PORT = 0x00000003;
public const int DBT_DEVTYP_VOLUME = 0x00000002;
public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_DEVICECHANGE)
{
//插入
if (wParam.ToInt32() == DBT_DEVICEARRIVAL)
{
var volume = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_HDR));
switch (volume.dbch_devicetype)
{
case DBT_DEVTYP_PORT://串口设备
string portName = Marshal.PtrToStringUni(lParam + Marshal.SizeOf(typeof(DEV_BROADCAST_PORT)));
break;
case DBT_DEVTYP_VOLUME:
var drive = GetDrive(lParam);
break;
default:
break;
}
}
//拔出
if (wParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)
{
var volume = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_HDR));
switch (volume.dbch_devicetype)
{
case DBT_DEVTYP_PORT://串口设备
string portName = Marshal.PtrToStringUni(lParam + Marshal.SizeOf(typeof(DEV_BROADCAST_PORT)));
break;
case DBT_DEVTYP_VOLUME:
var drive = GetDrive(lParam);
break;
default:
break;
}
}
}
return IntPtr.Zero;
}
private static string GetDrive(IntPtr lParam)
{
var volume = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_VOLUME));
var letter = GetLetter(volume.dbcv_unitmask);
return string.Format("{0}:\\", letter);
}
///
/// 获得盘符
///
///
/// 1 = A
/// 2 = B
/// 4 = C...
///
/// 结果是A~Z的任意一个字符或者为'?'
private static char GetLetter(uint dbcvUnitmask)
{
const char nona = '?';
const string drives = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (dbcvUnitmask == 0) return nona;
var i = 0;
var pom = dbcvUnitmask >> 1;
while (pom != 0)
{
pom = pom >> 1;
i++;
}
if (i < drives.Length)
return drives[i];
return nona;
}
#endregion
///
/// 垂直方向滚动到顶部
///
///
///
public static void ScrollViewToVerticalTop(FrameworkElement element, ScrollViewer scrollViewer)
{
var scrollViewerOffset = scrollViewer.VerticalOffset;
var point = new System.Windows.Point(0, scrollViewerOffset);
var tarPos = element.TransformToVisual(scrollViewer).Transform(point);
scrollViewer.ScrollToVerticalOffset(tarPos.Y);
}
///
/// 水平方向滚动到右侧
///
///
///
public static void ScrollViewToHorizontalRight(FrameworkElement element, ScrollViewer scrollViewer)
{
var scrollViewerOffset = scrollViewer.HorizontalOffset;
var point = new System.Windows.Point(scrollViewerOffset, 0);
var tarPos = element.TransformToVisual(scrollViewer).Transform(point);
scrollViewer.ScrollToHorizontalOffset(tarPos.X);
}
///
/// 汉字转全拼
///
///
///
public static string ConvertToAllSpell(string strChinese)
{
try
{
if (strChinese.Length != 0)
{
StringBuilder fullSpell = new StringBuilder();
for (int i = 0; i < strChinese.Length; i++)
{
var chr = strChinese[i];
//fullSpell.Append(GetSpell(chr));
}
return fullSpell.ToString().ToUpper();
}
}
catch (Exception e)
{
Console.WriteLine("出错!" + e.Message);
}
return string.Empty;
}
///
/// 汉字转首字母
///
///
///
public static string GetFirstSpell(string strChinese)
{
try
{
if (strChinese.Length != 0)
{
StringBuilder fullSpell = new StringBuilder();
for (int i = 0; i < strChinese.Length; i++)
{
var chr = strChinese[i];
//fullSpell.Append(GetSpell(chr)[0]);
}
return fullSpell.ToString().ToUpper();
}
}
catch (Exception e)
{
Console.WriteLine("出错!" + e.Message);
}
return string.Empty;
}
//private static string GetSpell(char chr)
//{
// var coverchr = NPinyin.Pinyin.GetPinyin(chr);
// return coverchr;
//}
}
[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_HDR
{
public UInt32 dbch_size;
public UInt32 dbch_devicetype;
public UInt32 dbch_reserved;
}
[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_PORT
{
public uint dbcp_size;
public uint dbcp_devicetype;
public uint dbcp_reserved;
}
[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_PORT_A
{
public uint dbcp_size;
public uint dbcp_devicetype;
public uint dbcp_reserved;
//public string dbcp_name;
}
[StructLayout(LayoutKind.Sequential)]
struct DEV_BROADCAST_VOLUME
{
/// DWORD->unsigned int
public uint dbcv_size;
/// DWORD->unsigned int
public uint dbcv_devicetype;
/// DWORD->unsigned int
public uint dbcv_reserved;
/// DWORD->unsigned int
public uint dbcv_unitmask;
/// WORD->unsigned short
public ushort dbcv_flags;
}
}