C#用註冊表開機自動啓動某某軟件

代碼如下:

     public static void chkAutoRun(bool isRun)
        {
            if (isRun)//開機自動啓動 
            {
                try
                {
                    RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                    runKey.SetValue("AutoRun.exe", System.Windows.Forms.Application.ExecutablePath);
                    runKey.Close();
                    this.Text = "註冊表修改自啓(已開啓)";
                }
                catch (IOException ie)
                {
                    MessageBox.Show(ie.Message);
                }
            }
            else  //不開機自動啓動註冊表信息刪除 
            {
                RegistryKey software = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                string[] aimnames = software.GetValueNames();
                foreach (string aimKey in aimnames)
                {
                    if (aimKey.Equals("AutoRun.exe"))
                    {
                        software.DeleteValue("AutoRun.exe");
                        software.Close();
                        break;
                    }
                }
                this.Text = "註冊表修改自啓(已關閉)";
            } 

        }


發佈了28 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章