AutoStartHelper.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MvvmScaffoldFrame48.DLL.SystemTools
  9. {
  10. public class AutoStartHelper
  11. {
  12. /// <summary>
  13. /// 若出现自启动时相对路径异常的情况可以在程序启动时引用如下代码
  14. /// Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
  15. /// </summary>
  16. #region 注册表实现(WIN11无效)
  17. // 注册表路径
  18. private static readonly string RegistryPath = @"Software\Microsoft\Windows\CurrentVersion\Run";
  19. // 应用名称,作为注册表键名
  20. private static readonly string AppName = "MvvmScffold";
  21. /// <summary>
  22. /// 获取当前可执行文件路径(带引号,用于注册表)
  23. /// </summary>
  24. public static string GetExecutablePathForRegistry()
  25. {
  26. string path = Process.GetCurrentProcess().MainModule.FileName;
  27. // 如果路径包含空格,必须用双引号包裹
  28. if (path.Contains(" "))
  29. {
  30. return $"\"{path}\"";
  31. }
  32. return path;
  33. }
  34. /// <summary>
  35. /// 获取当前可执行文件路径
  36. /// </summary>
  37. public static string GetExecutablePath()
  38. {
  39. return Process.GetCurrentProcess().MainModule.FileName;
  40. }
  41. /// <summary>
  42. /// 检查是否已设置自启动
  43. /// </summary>
  44. public static bool IsAutoStart()
  45. {
  46. using (var key = Registry.CurrentUser.OpenSubKey(RegistryPath, false))
  47. {
  48. if (key == null) return false;
  49. var value = key.GetValue(AppName);
  50. // 验证路径是否一致,防止exe移动后失效
  51. return value != null && value.ToString() == GetExecutablePath();
  52. }
  53. }
  54. /// <summary>
  55. /// 设置或取消自启动
  56. /// </summary>
  57. /// <param name="isAutoStart">true 为启用,false 为禁用</param>
  58. public static void SetAutoStart(bool isAutoStart)
  59. {
  60. using (var key = Registry.CurrentUser.OpenSubKey(RegistryPath, true))
  61. {
  62. if (key == null) return;
  63. if (isAutoStart)
  64. {
  65. if (!IsAutoStart())
  66. {
  67. key.SetValue(AppName, GetExecutablePathForRegistry());
  68. }
  69. }
  70. else
  71. {
  72. // false 参数表示值不存在时不抛出异常
  73. key.DeleteValue(AppName, false);
  74. }
  75. }
  76. }
  77. #endregion
  78. #region 任务计划程序实现(兼容性高,BUG未测试)
  79. private const string TaskName = "MvvmScffold";
  80. /// <summary>
  81. /// 检查任务是否已注册且处于启用状态
  82. /// </summary>
  83. /// <returns>true: 已注册且启用; false: 未注册或已禁用</returns>
  84. public static bool IsTaskRegisteredAndEnabled()
  85. {
  86. try
  87. {
  88. // 查询任务状态
  89. // /fo LIST 以列表格式输出
  90. // /v 显示详细信息
  91. string arguments = $"/query /tn \"{TaskName}\" /fo LIST /v";
  92. ProcessStartInfo startInfo = new ProcessStartInfo
  93. {
  94. FileName = "schtasks",
  95. Arguments = arguments,
  96. UseShellExecute = false,
  97. RedirectStandardOutput = true,
  98. RedirectStandardError = true,
  99. CreateNoWindow = true
  100. };
  101. using (Process process = Process.Start(startInfo))
  102. {
  103. string output = process.StandardOutput.ReadToEnd();
  104. process.WaitForExit();
  105. if (process.ExitCode == 0)
  106. {
  107. // 检查输出中是否包含 "Status: Ready" 或 "State: Enabled"
  108. // 不同语言版本的Windows输出可能略有不同,通常检查 "Ready" 或 "Enabled"
  109. // 英文系统: "Status: Ready"
  110. // 中文系统: "状态: 就绪"
  111. // 更通用的方法是检查是否存在该任务名,且没有报错
  112. // 如果需要严格判断是否启用,可以解析输出中的 "Status" 行
  113. if (output.Contains("Ready") || output.Contains("就绪"))
  114. {
  115. return true;
  116. }
  117. // 如果任务是 "Disabled" (禁用) 或 "Disabled" (中文: 已禁用),则返回 false
  118. if (output.Contains("Disabled") || output.Contains("已禁用"))
  119. {
  120. return false;
  121. }
  122. // 其他状态视为未准备好
  123. return false;
  124. }
  125. else
  126. {
  127. // 退出码非0,通常意味着任务不存在 (ERROR: The system cannot find the file specified.)
  128. return false;
  129. }
  130. }
  131. }
  132. catch (Exception)
  133. {
  134. // 发生异常时,保守地认为未注册,以便后续尝试重新注册
  135. return false;
  136. }
  137. }
  138. /// <summary>
  139. /// 创建或更新开机自启任务
  140. /// </summary>
  141. public static void RegisterTask()
  142. {
  143. // 1. 先检查是否已经正确配置
  144. if (IsTaskRegisteredAndEnabled())
  145. {
  146. // 可选:进一步检查路径是否一致,如果路径变了也需要更新
  147. // 这里为了简化,如果已启用则直接返回,避免重复弹窗
  148. Console.WriteLine("任务已存在且处于启用状态,无需操作。");
  149. return;
  150. }
  151. try
  152. {
  153. string exePath = Process.GetCurrentProcess().MainModule.FileName;
  154. // 构建 schtasks 命令
  155. // /ru "" 表示以当前用户运行 (在某些Win11版本中,显式指定用户更稳定)
  156. // /rl highest 表示以最高权限运行
  157. // /sc onlogon 表示登录时触发
  158. // /f 强制覆盖已存在的任务
  159. string arguments = $"/create /tn \"{TaskName}\" /tr \"\\\"{exePath}\\\"\" /sc onlogon /rl highest /f";
  160. ProcessStartInfo startInfo = new ProcessStartInfo
  161. {
  162. FileName = "schtasks",
  163. Arguments = arguments,
  164. UseShellExecute = false,
  165. RedirectStandardOutput = true,
  166. RedirectStandardError = true,
  167. Verb = "runas" // 请求管理员权限
  168. };
  169. using (Process process = Process.Start(startInfo))
  170. {
  171. process.WaitForExit();
  172. if (process.ExitCode == 0)
  173. {
  174. Console.WriteLine("任务计划创建/更新成功");
  175. }
  176. else
  177. {
  178. string error = process.StandardError.ReadToEnd();
  179. Console.WriteLine($"创建失败: {error}");
  180. }
  181. }
  182. }
  183. catch (Exception ex)
  184. {
  185. Console.WriteLine($"异常: {ex.Message}");
  186. }
  187. }
  188. /// <summary>
  189. /// 删除开机自启任务
  190. /// </summary>
  191. public static void UnregisterTask()
  192. {
  193. // 可选:先检查是否存在,避免无意义的弹窗
  194. try
  195. {
  196. string checkArgs = $"/query /tn \"{TaskName}\"";
  197. ProcessStartInfo checkInfo = new ProcessStartInfo("schtasks", checkArgs)
  198. {
  199. UseShellExecute = false,
  200. CreateNoWindow = true
  201. };
  202. using (var p = Process.Start(checkInfo))
  203. {
  204. p.WaitForExit();
  205. if (p.ExitCode != 0)
  206. {
  207. Console.WriteLine("任务不存在,无需删除。");
  208. return;
  209. }
  210. }
  211. }
  212. catch { }
  213. try
  214. {
  215. string arguments = $"/delete /tn \"{TaskName}\" /f";
  216. ProcessStartInfo startInfo = new ProcessStartInfo
  217. {
  218. FileName = "schtasks",
  219. Arguments = arguments,
  220. UseShellExecute = false,
  221. RedirectStandardOutput = true,
  222. RedirectStandardError = true,
  223. Verb = "runas"
  224. };
  225. using (Process process = Process.Start(startInfo))
  226. {
  227. process.WaitForExit();
  228. if (process.ExitCode == 0)
  229. {
  230. Console.WriteLine("任务删除成功");
  231. }
  232. else
  233. {
  234. string error = process.StandardError.ReadToEnd();
  235. Console.WriteLine($"删除失败: {error}");
  236. }
  237. }
  238. }
  239. catch (Exception ex)
  240. {
  241. Console.WriteLine($"删除任务失败: {ex.Message}");
  242. }
  243. }
  244. #endregion
  245. }
  246. }