1. 程式人生 > >WinForm設置註冊表自動啟動

WinForm設置註冊表自動啟動

thread tor spa emp dex final 註冊 reat open

string path = Application.StartupPath;
            SetAutoRun(path + @"\AppName.exe", true);

 /// <summary>
        /// 設置應用程序開機自動運行
        /// </summary>
        /// <param name="fileName">應用程序的文件名</param>
        /// <param name="isAutoRun">是否自動運行,為false時,取消自動運行</param>
/// <exception cref="system.Exception">設置不成功時拋出異常</exception> /// <returns>返回1成功,非1不成功</returns> public String SetAutoRun(string fileName, bool isAutoRun) { string reSet = string.Empty; RegistryKey reg = null;
try { if (!System.IO.File.Exists(fileName)) { reSet = "該文件不存在!"; } string name = fileName.Substring(fileName.LastIndexOf(@"\") + 1); reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run
", true); if (reg == null) { reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); } if (isAutoRun) { reg.SetValue(name, fileName); reSet = "1"; } else { reg.SetValue(name, false); } } catch (Exception ex) { threadMethod("設置自動啟動註冊表失敗:" + ex.Message); } finally { if (reg != null) { reg.Close(); } } return reSet; }

WinForm設置註冊表自動啟動