|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using Microsoft.Win32;
-
- using System;
-
- namespace Common.system
- {
-
-
-
- public static class AutoStartHelper
- {
-
-
-
-
-
-
- public static bool OpenAutoStart(string strName, out string Message)
- {
-
- #region 设置开机自启
- if (!System.IO.File.Exists(strName))
- {
- Message = "未检测到应用程序启动项。";
- return false;
- }
- string strnewName = strName.Substring(strName.LastIndexOf("\\") + 1);
- try
- {
- RegistryKey registry = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
- if (registry == null)
- registry = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
- registry.SetValue(strnewName, strName);
- Message = "";
- return true;
- }
- catch (Exception ex)
- {
- Message = "设置失败,操作被拒绝:" + ex.Message;
- LogHelper.WriteErrLog(Message, ex);
- return false;
- }
-
-
-
-
-
- #endregion
- }
-
-
-
-
-
-
- public static bool CloseAutoStart(string strName, out string Message)
- {
- #region 取消开机自启
- if (!System.IO.File.Exists(strName))
- {
- Message = "未检测到应用程序启动项。";
- return false;
- }
- string strnewName = strName.Substring(strName.LastIndexOf("\\") + 1);
- try
- {
- RegistryKey registry = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
- if (registry == null)
- registry = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
- registry.DeleteValue(strnewName, false);
- Message = "";
- return true;
- }
- catch (Exception ex)
- {
- Message = "设置失败,操作被拒绝:" + ex.Message;
- LogHelper.WriteErrLog(Message, ex);
- return false;
- }
-
-
-
-
- #endregion
- }
-
-
-
-
-
-
-
-
- public static bool OpenAutoStart1(string exeName, out string Message)
- {
-
- #region 设置开机自启
- try
- {
- Message = "";
- return true;
- }
- catch (Exception ex)
- {
- Message = "设置失败,操作被拒绝:" + ex.Message;
- LogHelper.WriteErrLog(Message, ex);
- return false;
- }
-
-
-
-
-
- #endregion
- }
- }
- }
|