??? public void SetReg()
??????? {
??? RegistryKey hklm=Registry.LocalMachine;
??? RegistryKey run=hklm.CreateSubKey(@"Software/Microsoft/Windows/CurrentVersion/Run"); //定義hklm指向注冊表的LocalMachine,對注冊表的結構,可以在windows的運行里,輸入regedit,運行后,可以看看里面的各 個子鍵,其中Software/Microsoft/Windows/CurrentVersion/Run就是關系到系統中隨系統啟動而啟動的程序,通 稱啟動項
??? try
??? {
???? run.SetValue("hello.exe",@"F:/c#/hello/bin/Debug/hello.exe"); //將我們的程序加進去,系統啟動時,hello.exe就會隨系統啟動而啟動了,后面F:/C#....就這個程序的位置,你可以將hello.exe 換成你自己的,比如:notepad.exe注意修改這個程序的位置。至于"@"這個符號加在"F:/C#/hello/"之前的作用,是為了保證. net編譯器,不將/解釋為轉換符,如果這里不用@的話,那就應該寫成"F://C#//hello//",一個/就要改為兩個//。
???? MessageBox.Show ("添加注冊表啟動項成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); //彈出信息框,提示,已經成功添加了。要了解MessageBox.Show的各參數意義,可以將光標放到其里面,按F1,.net的IDE(集成開發 環境)會有詳細的文檔顯示出來,告訴您最權威詳盡的解釋。
???? hklm.Close();} //注意,一定要關閉,注冊表應用。
??? catch(Exception my) //這是捕獲異常的
??? {
???? MessageBox.Show(my.Message.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
??? }??
???????
??????? }?
???????
?? private void button2_Click(object sender, System.EventArgs e) //button1是添加,這個button2是刪除。后面的實現都差不多
?? {
??
??? RegistryKey hklm=Registry.LocalMachine;
??? RegistryKey run=hklm.CreateSubKey(@"Software/Microsoft/Windows/CurrentVersion/Run");
??? try
??? {
???? run.DeleteValue("hello.exe"); //這兒是關鍵的區別,刪除hello.exe這個啟動項鍵值
??????????????????????????????
???? MessageBox.Show("移除注冊表啟動項成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
???? hklm.Close();
???? }
??? catch(Exception my)
??? {
???? MessageBox.Show(my.Message.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
??? }