|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- 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
- {
- /// <summary>
- /// 系统操作类
- /// </summary>
- public class SystemHelperNew
- {
- private volatile static SystemHelperNew _Instance;
- public static SystemHelperNew GetInstance => _Instance ?? (_Instance = new SystemHelperNew());
- private SystemHelperNew() { }
-
- /// <summary>
- /// 获取当前应用程序名称,包括后缀名
- /// </summary>
- public string GetApplicationName => $"{AppDomain.CurrentDomain.FriendlyName}.exe";
-
- /// <summary>
- /// 获取当前应用程序完整路径
- /// </summary>
- public string GetApplicationPath => $"{AppDomain.CurrentDomain.BaseDirectory}{GetApplicationName}";
-
- /// <summary>
- /// 创建桌面快捷方式
- /// </summary>
- /// <returns>成功或失败</returns>
- 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();
- }
- }
-
-
- /// <summary>
- /// 设置开机自启动
- /// </summary>
- /// <param name="isAuto">true:开机启动,false:不开机自启</param>
- 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();
- }
- }
-
- /// <summary>
- /// 判断是否是自动启动
- /// </summary>
- /// <returns></returns>
- 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);
- }
-
- /// <summary>
- /// 获得盘符
- /// </summary>
- /// <param name="dbcvUnitmask">
- /// 1 = A
- /// 2 = B
- /// 4 = C...
- /// </param>
- /// <returns>结果是A~Z的任意一个字符或者为'?'</returns>
- 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
-
- /// <summary>
- /// 垂直方向滚动到顶部
- /// </summary>
- /// <param name="element"></param>
- /// <param name="scrollViewer"></param>
- 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);
- }
-
- /// <summary>
- /// 水平方向滚动到右侧
- /// </summary>
- /// <param name="element"></param>
- /// <param name="scrollViewer"></param>
- 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);
- }
-
- /// <summary>
- /// 汉字转全拼
- /// </summary>
- /// <param name="strChinese"></param>
- /// <returns></returns>
- 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;
- }
-
- /// <summary>
- /// 汉字转首字母
- /// </summary>
- /// <param name="strChinese"></param>
- /// <returns></returns>
- 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;
- }
- }
|