终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

238 lines
8.8 KiB

  1. using IWshRuntimeLibrary;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace BPASmartClient.Helper
  11. {
  12. /// <summary>
  13. /// 系统操作类
  14. /// </summary>
  15. public class SystemHelper
  16. {
  17. private volatile static SystemHelper _Instance;
  18. public static SystemHelper GetInstance => _Instance ?? (_Instance = new SystemHelper());
  19. private SystemHelper() { }
  20. /// <summary>
  21. /// 获取当前应用程序名称,包括后缀名
  22. /// </summary>
  23. public string GetApplicationName => $"{AppDomain.CurrentDomain.FriendlyName}.exe";
  24. /// <summary>
  25. /// 获取当前应用程序完整路径
  26. /// </summary>
  27. public string GetApplicationPath => $"{AppDomain.CurrentDomain.BaseDirectory}{GetApplicationName}";
  28. /// <summary>
  29. /// 创建桌面快捷方式
  30. /// </summary>
  31. /// <returns>成功或失败</returns>
  32. public bool CreateDesktopShortcut()
  33. {
  34. //1、在COM对象中找到 Windows Script Host Object Model
  35. //2、添加引用 using IWshRuntimeLibrary;
  36. string deskTop = string.Empty;
  37. try
  38. {
  39. deskTop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\";
  40. if (System.IO.File.Exists(deskTop + GetApplicationName + ".lnk")) //
  41. {
  42. return true;
  43. //System.IO.File.Delete(deskTop + FileName + ".lnk");//删除原来的桌面快捷键方式
  44. }
  45. WshShell shell = new WshShell();
  46. //快捷键方式创建的位置、名称
  47. IWshShortcut shortcut = shell.CreateShortcut(deskTop + GetApplicationName + ".lnk") as IWshShortcut;
  48. shortcut.TargetPath = GetApplicationPath; //目标文件
  49. //该属性指定应用程序的工作目录,当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。
  50. shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
  51. shortcut.WindowStyle = 1; //目标应用程序的窗口状态分为普通、最大化、最小化【1,3,7】
  52. shortcut.Description = GetApplicationName; //描述
  53. //shortcut.IconLocation = exePath + "\\logo.ico"; //快捷方式图标
  54. shortcut.Arguments = "";
  55. //shortcut.Hotkey = "CTRL+ALT+F11"; // 快捷键
  56. shortcut.Save(); //必须调用保存快捷才成创建成功
  57. return true;
  58. }
  59. catch (Exception ex)
  60. {
  61. MessageLog.GetInstance.ShowEx(ex.ToString());
  62. return false;
  63. }
  64. }
  65. /// <summary>
  66. /// 设置开机自启动
  67. /// </summary>
  68. /// <param name="isAuto">true:开机启动,false:不开机自启</param>
  69. public void AutoStart(bool isAuto = true)
  70. {
  71. if (isAuto == true)
  72. {
  73. RegistryKey R_local = Registry.CurrentUser;
  74. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  75. R_run.SetValue(GetApplicationName, GetApplicationPath);
  76. R_run.Close();
  77. R_local.Close();
  78. }
  79. else
  80. {
  81. RegistryKey R_local = Registry.CurrentUser;
  82. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  83. R_run.DeleteValue(GetApplicationName, false);
  84. R_run.Close();
  85. R_local.Close();
  86. }
  87. }
  88. /// <summary>
  89. /// 判断是否是自动启动
  90. /// </summary>
  91. /// <returns></returns>
  92. public bool IsAutoStart()
  93. {
  94. RegistryKey R_local = Registry.CurrentUser;
  95. RegistryKey R_run = R_local.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  96. bool res = R_run.GetValueNames().Contains(GetApplicationName);
  97. R_run.Close();
  98. R_local.Close();
  99. return res;
  100. }
  101. #region U盘,串口插拔信息
  102. private const int WM_DEVICECHANGE = 0x219; //设备改变
  103. private const int DBT_DEVICEARRIVAL = 0x8000; //检测到新设备
  104. private const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //移除设备
  105. public const int DBT_DEVTYP_PORT = 0x00000003;
  106. public const int DBT_DEVTYP_VOLUME = 0x00000002;
  107. public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  108. {
  109. if (msg == WM_DEVICECHANGE)
  110. {
  111. //插入
  112. if (wParam.ToInt32() == DBT_DEVICEARRIVAL)
  113. {
  114. var volume = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_HDR));
  115. switch (volume.dbch_devicetype)
  116. {
  117. case DBT_DEVTYP_PORT://串口设备
  118. string portName = Marshal.PtrToStringUni(lParam + Marshal.SizeOf(typeof(DEV_BROADCAST_PORT)));
  119. MessageLog.GetInstance.Show($"插入串口:[{portName}]");
  120. break;
  121. case DBT_DEVTYP_VOLUME:
  122. var drive = GetDrive(lParam);
  123. MessageLog.GetInstance.Show($"usb插入:[{drive}]");
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. //拔出
  130. if (wParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)
  131. {
  132. var volume = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_HDR));
  133. switch (volume.dbch_devicetype)
  134. {
  135. case DBT_DEVTYP_PORT://串口设备
  136. string portName = Marshal.PtrToStringUni(lParam + Marshal.SizeOf(typeof(DEV_BROADCAST_PORT)));
  137. MessageLog.GetInstance.Show($"拔出串口:[{portName}]");
  138. break;
  139. case DBT_DEVTYP_VOLUME:
  140. var drive = GetDrive(lParam);
  141. MessageLog.GetInstance.Show($"usb拔出:[{drive}]");
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. }
  148. return IntPtr.Zero;
  149. }
  150. private static string GetDrive(IntPtr lParam)
  151. {
  152. var volume = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_VOLUME));
  153. var letter = GetLetter(volume.dbcv_unitmask);
  154. return string.Format("{0}:\\", letter);
  155. }
  156. /// <summary>
  157. /// 获得盘符
  158. /// </summary>
  159. /// <param name="dbcvUnitmask">
  160. /// 1 = A
  161. /// 2 = B
  162. /// 4 = C...
  163. /// </param>
  164. /// <returns>结果是A~Z的任意一个字符或者为'?'</returns>
  165. private static char GetLetter(uint dbcvUnitmask)
  166. {
  167. const char nona = '?';
  168. const string drives = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  169. if (dbcvUnitmask == 0) return nona;
  170. var i = 0;
  171. var pom = dbcvUnitmask >> 1;
  172. while (pom != 0)
  173. {
  174. pom = pom >> 1;
  175. i++;
  176. }
  177. if (i < drives.Length)
  178. return drives[i];
  179. return nona;
  180. }
  181. #endregion
  182. }
  183. [StructLayout(LayoutKind.Sequential)]
  184. struct DEV_BROADCAST_HDR
  185. {
  186. public UInt32 dbch_size;
  187. public UInt32 dbch_devicetype;
  188. public UInt32 dbch_reserved;
  189. }
  190. [StructLayout(LayoutKind.Sequential)]
  191. struct DEV_BROADCAST_PORT
  192. {
  193. public uint dbcp_size;
  194. public uint dbcp_devicetype;
  195. public uint dbcp_reserved;
  196. }
  197. [StructLayout(LayoutKind.Sequential)]
  198. struct DEV_BROADCAST_PORT_A
  199. {
  200. public uint dbcp_size;
  201. public uint dbcp_devicetype;
  202. public uint dbcp_reserved;
  203. //public string dbcp_name;
  204. }
  205. [StructLayout(LayoutKind.Sequential)]
  206. struct DEV_BROADCAST_VOLUME
  207. {
  208. /// DWORD->unsigned int
  209. public uint dbcv_size;
  210. /// DWORD->unsigned int
  211. public uint dbcv_devicetype;
  212. /// DWORD->unsigned int
  213. public uint dbcv_reserved;
  214. /// DWORD->unsigned int
  215. public uint dbcv_unitmask;
  216. /// WORD->unsigned short
  217. public ushort dbcv_flags;
  218. }
  219. }