星火微课系统客户端
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.

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