终端一体化运控平台
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

349 linhas
13 KiB

  1. using IWshRuntimeLibrary;
  2. using Microsoft.Win32;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. namespace BeDesignerSCADA.Helper
  15. {
  16. /// <summary>
  17. /// 系统操作类
  18. /// </summary>
  19. public class SystemHelperNew
  20. {
  21. private volatile static SystemHelperNew _Instance;
  22. public static SystemHelperNew GetInstance => _Instance ?? (_Instance = new SystemHelperNew());
  23. private SystemHelperNew() { }
  24. /// <summary>
  25. /// 获取当前应用程序名称,包括后缀名
  26. /// </summary>
  27. public string GetApplicationName => $"{AppDomain.CurrentDomain.FriendlyName}.exe";
  28. /// <summary>
  29. /// 获取当前应用程序完整路径
  30. /// </summary>
  31. public string GetApplicationPath => $"{AppDomain.CurrentDomain.BaseDirectory}{GetApplicationName}";
  32. /// <summary>
  33. /// 创建桌面快捷方式
  34. /// </summary>
  35. /// <returns>成功或失败</returns>
  36. public void CreateShortcutOnDesktop()
  37. {
  38. //添加引用 (com->Windows Script Host Object Model),using IWshRuntimeLibrary;
  39. String shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), $"{AppDomain.CurrentDomain.FriendlyName}.lnk");
  40. if (!System.IO.File.Exists(shortcutPath))
  41. {
  42. // 获取当前应用程序目录地址
  43. String exePath = Process.GetCurrentProcess().MainModule.FileName;
  44. IWshShell shell = new WshShell();
  45. // 确定是否已经创建的快捷键被改名了
  46. foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk"))
  47. {
  48. WshShortcut tempShortcut = (WshShortcut)shell.CreateShortcut(item);
  49. if (tempShortcut.TargetPath == exePath)
  50. {
  51. return;
  52. }
  53. }
  54. WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath);
  55. shortcut.TargetPath = exePath;
  56. shortcut.Arguments = "";// 参数
  57. shortcut.Description = AppDomain.CurrentDomain.FriendlyName;
  58. shortcut.WorkingDirectory = Environment.CurrentDirectory;//程序所在文件夹,在快捷方式图标点击右键可以看到此属性
  59. shortcut.IconLocation = exePath;//图标,该图标是应用程序的资源文件
  60. //shortcut.Hotkey = "CTRL+SHIFT+W";//热键,发现没作用,大概需要注册一下
  61. shortcut.WindowStyle = 1;
  62. shortcut.Save();
  63. }
  64. }
  65. public void CreateShortcutOnDesktop(string Name)
  66. {
  67. //添加引用 (com->Windows Script Host Object Model),using IWshRuntimeLibrary;
  68. String shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), $"{Name}.lnk");
  69. if (!System.IO.File.Exists(shortcutPath))
  70. {
  71. // 获取当前应用程序目录地址
  72. String exePath = $"{AppDomain.CurrentDomain.BaseDirectory}{Name}.exe";// Process.GetCurrentProcess().MainModule.FileName;
  73. IWshShell shell = new WshShell();
  74. // 确定是否已经创建的快捷键被改名了
  75. foreach (var item in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.lnk"))
  76. {
  77. WshShortcut tempShortcut = (WshShortcut)shell.CreateShortcut(item);
  78. if (tempShortcut.TargetPath == exePath)
  79. {
  80. return;
  81. }
  82. }
  83. WshShortcut shortcut = (WshShortcut)shell.CreateShortcut(shortcutPath);
  84. shortcut.TargetPath = exePath;
  85. shortcut.Arguments = "";// 参数
  86. shortcut.Description = AppDomain.CurrentDomain.FriendlyName;
  87. shortcut.WorkingDirectory = Environment.CurrentDirectory;//程序所在文件夹,在快捷方式图标点击右键可以看到此属性
  88. shortcut.IconLocation = exePath;//图标,该图标是应用程序的资源文件
  89. //shortcut.Hotkey = "CTRL+SHIFT+W";//热键,发现没作用,大概需要注册一下
  90. shortcut.WindowStyle = 1;
  91. shortcut.Save();
  92. }
  93. }
  94. /// <summary>
  95. /// 设置开机自启动
  96. /// </summary>
  97. /// <param name="isAuto">true:开机启动,false:不开机自启</param>
  98. public void AutoStart(bool isAuto = true)
  99. {
  100. if (isAuto == true)
  101. {
  102. RegistryKey R_local = Registry.CurrentUser;
  103. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  104. R_run.SetValue(GetApplicationName, GetApplicationPath);
  105. R_run.Close();
  106. R_local.Close();
  107. }
  108. else
  109. {
  110. RegistryKey R_local = Registry.CurrentUser;
  111. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  112. R_run.DeleteValue(GetApplicationName, false);
  113. R_run.Close();
  114. R_local.Close();
  115. }
  116. }
  117. /// <summary>
  118. /// 判断是否是自动启动
  119. /// </summary>
  120. /// <returns></returns>
  121. public bool IsAutoStart()
  122. {
  123. RegistryKey R_local = Registry.CurrentUser;
  124. RegistryKey R_run = R_local.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  125. bool res = R_run.GetValueNames().Contains(GetApplicationName);
  126. R_run.Close();
  127. R_local.Close();
  128. return res;
  129. }
  130. #region U盘,串口插拔信息
  131. private const int WM_DEVICECHANGE = 0x219; //设备改变
  132. private const int DBT_DEVICEARRIVAL = 0x8000; //检测到新设备
  133. private const int DBT_DEVICEREMOVECOMPLETE = 0x8004; //移除设备
  134. public const int DBT_DEVTYP_PORT = 0x00000003;
  135. public const int DBT_DEVTYP_VOLUME = 0x00000002;
  136. public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  137. {
  138. if (msg == WM_DEVICECHANGE)
  139. {
  140. //插入
  141. if (wParam.ToInt32() == DBT_DEVICEARRIVAL)
  142. {
  143. var volume = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_HDR));
  144. switch (volume.dbch_devicetype)
  145. {
  146. case DBT_DEVTYP_PORT://串口设备
  147. string portName = Marshal.PtrToStringUni(lParam + Marshal.SizeOf(typeof(DEV_BROADCAST_PORT)));
  148. break;
  149. case DBT_DEVTYP_VOLUME:
  150. var drive = GetDrive(lParam);
  151. break;
  152. default:
  153. break;
  154. }
  155. }
  156. //拔出
  157. if (wParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)
  158. {
  159. var volume = (DEV_BROADCAST_HDR)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_HDR));
  160. switch (volume.dbch_devicetype)
  161. {
  162. case DBT_DEVTYP_PORT://串口设备
  163. string portName = Marshal.PtrToStringUni(lParam + Marshal.SizeOf(typeof(DEV_BROADCAST_PORT)));
  164. break;
  165. case DBT_DEVTYP_VOLUME:
  166. var drive = GetDrive(lParam);
  167. break;
  168. default:
  169. break;
  170. }
  171. }
  172. }
  173. return IntPtr.Zero;
  174. }
  175. private static string GetDrive(IntPtr lParam)
  176. {
  177. var volume = (DEV_BROADCAST_VOLUME)Marshal.PtrToStructure(lParam, typeof(DEV_BROADCAST_VOLUME));
  178. var letter = GetLetter(volume.dbcv_unitmask);
  179. return string.Format("{0}:\\", letter);
  180. }
  181. /// <summary>
  182. /// 获得盘符
  183. /// </summary>
  184. /// <param name="dbcvUnitmask">
  185. /// 1 = A
  186. /// 2 = B
  187. /// 4 = C...
  188. /// </param>
  189. /// <returns>结果是A~Z的任意一个字符或者为'?'</returns>
  190. private static char GetLetter(uint dbcvUnitmask)
  191. {
  192. const char nona = '?';
  193. const string drives = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  194. if (dbcvUnitmask == 0) return nona;
  195. var i = 0;
  196. var pom = dbcvUnitmask >> 1;
  197. while (pom != 0)
  198. {
  199. pom = pom >> 1;
  200. i++;
  201. }
  202. if (i < drives.Length)
  203. return drives[i];
  204. return nona;
  205. }
  206. #endregion
  207. /// <summary>
  208. /// 垂直方向滚动到顶部
  209. /// </summary>
  210. /// <param name="element"></param>
  211. /// <param name="scrollViewer"></param>
  212. public static void ScrollViewToVerticalTop(FrameworkElement element, ScrollViewer scrollViewer)
  213. {
  214. var scrollViewerOffset = scrollViewer.VerticalOffset;
  215. var point = new System.Windows.Point(0, scrollViewerOffset);
  216. var tarPos = element.TransformToVisual(scrollViewer).Transform(point);
  217. scrollViewer.ScrollToVerticalOffset(tarPos.Y);
  218. }
  219. /// <summary>
  220. /// 水平方向滚动到右侧
  221. /// </summary>
  222. /// <param name="element"></param>
  223. /// <param name="scrollViewer"></param>
  224. public static void ScrollViewToHorizontalRight(FrameworkElement element, ScrollViewer scrollViewer)
  225. {
  226. var scrollViewerOffset = scrollViewer.HorizontalOffset;
  227. var point = new System.Windows.Point(scrollViewerOffset, 0);
  228. var tarPos = element.TransformToVisual(scrollViewer).Transform(point);
  229. scrollViewer.ScrollToHorizontalOffset(tarPos.X);
  230. }
  231. /// <summary>
  232. /// 汉字转全拼
  233. /// </summary>
  234. /// <param name="strChinese"></param>
  235. /// <returns></returns>
  236. public static string ConvertToAllSpell(string strChinese)
  237. {
  238. try
  239. {
  240. if (strChinese.Length != 0)
  241. {
  242. StringBuilder fullSpell = new StringBuilder();
  243. for (int i = 0; i < strChinese.Length; i++)
  244. {
  245. var chr = strChinese[i];
  246. //fullSpell.Append(GetSpell(chr));
  247. }
  248. return fullSpell.ToString().ToUpper();
  249. }
  250. }
  251. catch (Exception e)
  252. {
  253. Console.WriteLine("出错!" + e.Message);
  254. }
  255. return string.Empty;
  256. }
  257. /// <summary>
  258. /// 汉字转首字母
  259. /// </summary>
  260. /// <param name="strChinese"></param>
  261. /// <returns></returns>
  262. public static string GetFirstSpell(string strChinese)
  263. {
  264. try
  265. {
  266. if (strChinese.Length != 0)
  267. {
  268. StringBuilder fullSpell = new StringBuilder();
  269. for (int i = 0; i < strChinese.Length; i++)
  270. {
  271. var chr = strChinese[i];
  272. //fullSpell.Append(GetSpell(chr)[0]);
  273. }
  274. return fullSpell.ToString().ToUpper();
  275. }
  276. }
  277. catch (Exception e)
  278. {
  279. Console.WriteLine("出错!" + e.Message);
  280. }
  281. return string.Empty;
  282. }
  283. //private static string GetSpell(char chr)
  284. //{
  285. // var coverchr = NPinyin.Pinyin.GetPinyin(chr);
  286. // return coverchr;
  287. //}
  288. }
  289. [StructLayout(LayoutKind.Sequential)]
  290. struct DEV_BROADCAST_HDR
  291. {
  292. public UInt32 dbch_size;
  293. public UInt32 dbch_devicetype;
  294. public UInt32 dbch_reserved;
  295. }
  296. [StructLayout(LayoutKind.Sequential)]
  297. struct DEV_BROADCAST_PORT
  298. {
  299. public uint dbcp_size;
  300. public uint dbcp_devicetype;
  301. public uint dbcp_reserved;
  302. }
  303. [StructLayout(LayoutKind.Sequential)]
  304. struct DEV_BROADCAST_PORT_A
  305. {
  306. public uint dbcp_size;
  307. public uint dbcp_devicetype;
  308. public uint dbcp_reserved;
  309. //public string dbcp_name;
  310. }
  311. [StructLayout(LayoutKind.Sequential)]
  312. struct DEV_BROADCAST_VOLUME
  313. {
  314. /// DWORD->unsigned int
  315. public uint dbcv_size;
  316. /// DWORD->unsigned int
  317. public uint dbcv_devicetype;
  318. /// DWORD->unsigned int
  319. public uint dbcv_reserved;
  320. /// DWORD->unsigned int
  321. public uint dbcv_unitmask;
  322. /// WORD->unsigned short
  323. public ushort dbcv_flags;
  324. }
  325. }