星火微课系统客户端
Você não pode selecionar mais de 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.

AutoStartHelper.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Common.system
  8. {
  9. /// <summary>
  10. /// 自启帮助类
  11. /// </summary>
  12. public static class AutoStartHelper
  13. {
  14. /// <summary>
  15. /// 开机自启动
  16. /// </summary>
  17. /// <param name="strName">需要启动软件的路径</param>
  18. /// <param name="Message">错误信息</param>
  19. /// <returns></returns>
  20. public static bool OpenAutoStart(string strName,out string Message)
  21. {
  22. #region 设置开机自启
  23. if (!System.IO.File.Exists(strName))//判断要自动运行的应用程序文件是否存在
  24. {
  25. Message = "未检测到应用程序启动项。";
  26. return false;
  27. }
  28. string strnewName = strName.Substring(strName.LastIndexOf("\\") + 1);//获取应用程序文件名,不包括路径
  29. try
  30. {
  31. RegistryKey registry = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//检索指定的子项
  32. if (registry == null)//若指定的子项不存在
  33. registry = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");//则创建指定的子项
  34. registry.SetValue(strnewName, strName);//设置该子项的新的“键值对”
  35. Message = "";
  36. return true;
  37. }
  38. catch (Exception ex)
  39. {
  40. Message = "设置失败,操作被拒绝:" + ex.Message;
  41. LogHelper.WriteErrLog(Message, ex);
  42. return false;
  43. }
  44. //if (MessageBox.Show("设置完毕") == DialogResult.OK)
  45. //{
  46. // RefreshSystem();//刷新系统
  47. //}
  48. #endregion
  49. }
  50. /// <summary>
  51. /// 关闭自启
  52. /// </summary>
  53. /// <param name="strName">软件名称</param>
  54. /// <param name="Message">错误消息</param>
  55. /// <returns></returns>
  56. public static bool CloseAutoStart(string strName, out string Message)
  57. {
  58. #region 取消开机自启
  59. if (!System.IO.File.Exists(strName))//判断要取消的应用程序文件是否存在
  60. {
  61. Message = "未检测到应用程序启动项。";
  62. return false;
  63. }
  64. string strnewName = strName.Substring(strName.LastIndexOf("\\") + 1);///获取应用程序文件名,不包括路径
  65. try
  66. {
  67. RegistryKey registry = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//读取指定的子项
  68. if (registry == null)//若指定的子项不存在
  69. registry = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");//则创建指定的子项
  70. registry.DeleteValue(strnewName, false);//删除指定“键名称”的键/值对
  71. Message = "";
  72. return true;
  73. }
  74. catch (Exception ex)
  75. {
  76. Message = "设置失败,操作被拒绝:"+ex.Message;
  77. LogHelper.WriteErrLog(Message, ex);
  78. return false;
  79. }
  80. //if (MessageBox.Show("设置完毕") == DialogResult.OK)
  81. //{
  82. // RefreshSystem();
  83. //}
  84. #endregion
  85. }
  86. /// <summary>
  87. /// 开机自启动
  88. /// </summary>
  89. /// <param name="strName">程序名称</param>
  90. /// <param name="Message">错误信息</param>
  91. /// <returns></returns>
  92. public static bool OpenAutoStart1(string exeName, out string Message)
  93. {
  94. #region 设置开机自启
  95. try
  96. {
  97. Message = "";
  98. return true;
  99. }
  100. catch (Exception ex)
  101. {
  102. Message = "设置失败,操作被拒绝:" + ex.Message;
  103. LogHelper.WriteErrLog(Message, ex);
  104. return false;
  105. }
  106. //if (MessageBox.Show("设置完毕") == DialogResult.OK)
  107. //{
  108. // RefreshSystem();//刷新系统
  109. //}
  110. #endregion
  111. }
  112. }
  113. }