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.
 
 

111 lines
4.2 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.Text;
  8. using System.Threading.Tasks;
  9. namespace HBLConsole.Service
  10. {
  11. /// <summary>
  12. /// 系统操作类
  13. /// </summary>
  14. public class SystemHelper
  15. {
  16. private volatile static SystemHelper _Instance;
  17. public static SystemHelper GetInstance => _Instance ?? (_Instance = new SystemHelper());
  18. private SystemHelper() { }
  19. /// <summary>
  20. /// 获取当前应用程序名称,包括后缀名
  21. /// </summary>
  22. public string GetApplicationName => $"{AppDomain.CurrentDomain.FriendlyName}.exe";
  23. /// <summary>
  24. /// 获取当前应用程序完整路径
  25. /// </summary>
  26. public string GetApplicationPath => $"{AppDomain.CurrentDomain.BaseDirectory}{GetApplicationName}";
  27. /// <summary>
  28. /// 创建桌面快捷方式
  29. /// </summary>
  30. /// <returns>成功或失败</returns>
  31. public bool CreateDesktopShortcut()
  32. {
  33. //1、在COM对象中找到 Windows Script Host Object Model
  34. //2、添加引用 using IWshRuntimeLibrary;
  35. string deskTop = string.Empty;
  36. try
  37. {
  38. deskTop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\";
  39. if (System.IO.File.Exists(deskTop + GetApplicationName + ".lnk")) //
  40. {
  41. return true;
  42. //System.IO.File.Delete(deskTop + FileName + ".lnk");//删除原来的桌面快捷键方式
  43. }
  44. WshShell shell = new WshShell();
  45. //快捷键方式创建的位置、名称
  46. IWshShortcut shortcut = shell.CreateShortcut(deskTop + GetApplicationName + ".lnk") as IWshShortcut;
  47. shortcut.TargetPath = GetApplicationPath; //目标文件
  48. //该属性指定应用程序的工作目录,当用户没有指定一个具体的目录时,快捷方式的目标应用程序将使用该属性所指定的目录来装载或保存文件。
  49. shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
  50. shortcut.WindowStyle = 1; //目标应用程序的窗口状态分为普通、最大化、最小化【1,3,7】
  51. shortcut.Description = GetApplicationName; //描述
  52. //shortcut.IconLocation = exePath + "\\logo.ico"; //快捷方式图标
  53. shortcut.Arguments = "";
  54. //shortcut.Hotkey = "CTRL+ALT+F11"; // 快捷键
  55. shortcut.Save(); //必须调用保存快捷才成创建成功
  56. return true;
  57. }
  58. catch (Exception)
  59. {
  60. return false;
  61. }
  62. }
  63. /// <summary>
  64. /// 设置开机自启动
  65. /// </summary>
  66. /// <param name="isAuto">true:开机启动,false:不开机自启</param>
  67. public void AutoStart(bool isAuto = true)
  68. {
  69. if (isAuto == true)
  70. {
  71. RegistryKey R_local = Registry.CurrentUser;
  72. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  73. R_run.SetValue(GetApplicationName, GetApplicationPath);
  74. R_run.Close();
  75. R_local.Close();
  76. }
  77. else
  78. {
  79. RegistryKey R_local = Registry.CurrentUser;
  80. RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  81. R_run.DeleteValue(GetApplicationName, false);
  82. R_run.Close();
  83. R_local.Close();
  84. }
  85. }
  86. /// <summary>
  87. /// 判断是否是自动启动
  88. /// </summary>
  89. /// <returns></returns>
  90. public bool IsAutoStart()
  91. {
  92. RegistryKey R_local = Registry.CurrentUser;
  93. RegistryKey R_run = R_local.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  94. bool res = R_run.GetValueNames().Contains(GetApplicationName);
  95. R_run.Close();
  96. R_local.Close();
  97. return res;
  98. }
  99. }
  100. }