终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

239 rindas
8.8 KiB

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