相信每個人對注冊表並不陌生,在運行裡面輸入“regedit”就可以打開注冊表編輯器了。這東西對Windows系統來說可是比較重要的,也是病 毒常常會光顧的地方,比如病毒和惡意軟件常常會在注冊表的啟動項裡面寫入自己的啟動鍵值來達到自啟動的目的,有些病毒還會修改注冊表裡面來映像劫持殺毒軟 件,這是破壞系統的第一步。同時,大多軟件(軟件的序列號和信息)和硬件信息、系統信息、安全模式等等設置都保存在這裡,因此系統的健康在很大程度上要依 賴注冊表的健康。
作為編程開發人員,我們有必要了解注冊表並學會操作注冊表。下面我們就來用.NET下托管語言C#操作注冊表,主要內容包括:注冊表項的創建,打開與刪除、鍵值的創建(設置值、修改),讀取和刪除、判斷注冊表項是否存在、判斷鍵值是否存在。
准備工作:
1:要操作注冊表,我們必須要引入必要的命名空間:
C#代碼 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=using%20Microsoft.Win32%3B%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
- using Microsoft.Win32;
C#代碼
- using Microsoft.Win32;
在這個命名空間裡面包含了許多注冊表相關的類,足夠我們使用了~~
2:命名空間裡面提供了一個類:RegistryKey 利用它我們可以定位到注冊表最開頭的分支:
ClassesRoot,CurrentUser,Users,LocalMachine,CurrentConfig
如:RegistryKey key = Registry.LocalMachine;
3:在操作的過程中涉及到子分支,要用\\進行深入,單個\會報錯!
4:最後要調用RegistryKey對象的Close()關閉對注冊表的修改~~~
5:以下我們的例子都是在LocalMachine分支下,請注意。
一:注冊表項的創建,打開與刪除
1:創建:
創建注冊表項主要用到RegistryKey 的CreateSubKey()方法。如:
C#代碼 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=RegistryKey%20key%20%3D%20Registry.LocalMachine%3B%0A%0ARegistryKey%20software%20%3D%20key.CreateSubKey(%22software%5C%5Ctest%22)%3B%20%20%0A%0A%2F%2F%E5%9C%A8HKEY_LOCAL_MACHINE%5CSOFTWARE%E4%B8%8B%E6%96%B0%E5%BB%BA%E5%90%8D%E4%B8%BAtest%E7%9A%84%E6%B3%A8%E5%86%8C%E8%A1%A8%E9%A1%B9%E3%80%82%E5%A6%82%E6%9E%9C%E5%B7%B2%E7%BB%8F%E5%AD%98%E5%9C%A8%E5%88%99%E4%B8%8D%E5%BD%B1%E5%93%8D%EF%BC%81%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
- RegistryKey key = Registry.LocalMachine;
-
- RegistryKey software = key.CreateSubKey("software\\test" );
-
- //在HKEY_LOCAL_MACHINE\SOFTWARE下新建名為test的注冊表項。如果已經存在則不影響!
C#代碼
- RegistryKey key = Registry.LocalMachine;
-
- RegistryKey software = key.CreateSubKey("software\\test");
-
- //在HKEY_LOCAL_MACHINE\SOFTWARE下新建名為test的注冊表項。如果已經存在則不影響!
2:打開:
打開注冊表項主要用到RegistryKey 的OpenSubKey()方法。如:
C#代碼 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=RegistryKey%20key%20%3D%20Registry.LocalMachine%3B%0A%0ARegistryKey%20software%20%3D%20key.OpenSubKey(%22software%5C%5Ctest%22%2Ctrue)%3B%20%20%0A%0A%2F%2F%E6%B3%A8%E6%84%8F%E8%AF%A5%E6%96%B9%E6%B3%95%E5%90%8E%E9%9D%A2%E8%BF%98%E5%8F%AF%E4%BB%A5%E6%9C%89%E4%B8%80%E4%B8%AA%E5%B8%83%E5%B0%94%E5%9E%8B%E7%9A%84%E5%8F%82%E6%95%B0%EF%BC%8Ctrue%E8%A1%A8%E7%A4%BA%E5%8F%AF%E4%BB%A5%E5%86%99%E5%85%A5%E3%80%82%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
- RegistryKey key = Registry.LocalMachine;
-
- RegistryKey software = key.OpenSubKey("software\\test" , true );
-
- //注意該方法後面還可以有一個布爾型的參數,true表示可以寫入。
C#代碼
- RegistryKey key = Registry.LocalMachine;
-
- RegistryKey software = key.OpenSubKey("software\\test",true);
-
- //注意該方法後面還可以有一個布爾型的參數,true表示可以寫入。
注意,如果該注冊表項不存在,這調用這個方法會拋出異常
3:刪除:
刪除注冊表項主要用到RegistryKey 的DeleteSubKey()方法。如:
C#代碼 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=RegistryKey%20key%20%3D%20Registry.LocalMachine%3B%0A%0Akey.DeleteSubKey(%22software%5C%5Ctest%22%2Ctrue)%3B%20%20%20%20%2F%2F%E8%AF%A5%E6%96%B9%E6%B3%95%E6%97%A0%E8%BF%94%E5%9B%9E%E5%80%BC%EF%BC%8C%E7%9B%B4%E6%8E%A5%E8%B0%83%E7%94%A8%E5%8D%B3%E5%8F%AF%0A%0Akey.Close()%3B%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
- RegistryKey key = Registry.LocalMachine;
-
- key.DeleteSubKey("software\\test" , true ); //該方法無返回值,直接調用即可
-
- key.Close();
C#代碼
- RegistryKey key = Registry.LocalMachine;
-
- key.DeleteSubKey("software\\test",true); //該方法無返回值,直接調用即可
-
- key.Close();
注意,如果該注冊表項不存在,這調用這個方法會拋出異常
二:鍵值的創建(設置值、修改),讀取和刪除
1:創建(設置值、修改):
對鍵值的創建修改等操作主要用到RegistryKey 的SetValue()方法
C#代碼 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=RegistryKey%20key%20%3D%20Registry.LocalMachine%3B%0A%0ARegistryKey%20software%20%3D%20key.OpenSubKey(%22software%5C%5Ctest%22%2Ctrue)%3B%20%2F%2F%E8%AF%A5%E9%A1%B9%E5%BF%85%E9%A1%BB%E5%B7%B2%E5%AD%98%E5%9C%A8%0A%0Asoftware.SetValue(%22test%22%2C%20%22coolszy%22)%3B%20%0A%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
- RegistryKey key = Registry.LocalMachine;
-
- RegistryKey software = key.OpenSubKey("software\\test" , true ); //該項必須已存在
-
- software.SetValue("test" , "coolszy" );
C#代碼
- RegistryKey key = Registry.LocalMachine;
-
- RegistryKey software = key.OpenSubKey("software\\test",true); //該項必須已存在
-
- software.SetValue("test", "coolszy");
//在HKEY_LOCAL_MACHINE\SOFTWARE\test下創建一個名為“test”,值為“coolszy”的鍵值。如果該鍵值原本已經存在,則會修改替換原來的鍵值,如果不存在則是創建該鍵值。
// 注意:SetValue()還有第三個參數,主要是用於設置鍵值的類型,如:字符串,二進制,Dword等等~~默認是字符串。如:
// software.SetValue("test", "0", RegistryValueKind.DWord); //二進制信息
Key.Close();
2:讀取:
C#代碼 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=string%20info%20%3D%20%22%22%3B%0A%0ARegistryKey%20Key%3B%0A%0AKey%20%3D%20Registry.LocalMachine%3B%0A%0Amyreg%20%3D%20Key.OpenSubKey(%22software%5C%5Ctest%22)%3B%20%20%0A%0A%2F%2F%20myreg%20%3D%20Key.OpenSubKey(%22software%5C%5Ctest%22%2Ctrue)%3B%0A%0Ainfo%20%3D%20myreg.GetValue(%22test%22).ToString()%3B%0A%0Amyreg.Close()%3B" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
- string info = "" ;
-
- RegistryKey Key;
-
- Key = Registry.LocalMachine;
-
- myreg = Key.OpenSubKey("software\\test" );
-
- // myreg = Key.OpenSubKey("software\\test",true);
-
- info = myreg.GetValue("test" ).ToString();
-
- myreg.Close();
C#代碼
- string info = "";
-
- RegistryKey Key;
-
- Key = Registry.LocalMachine;
-
- myreg = Key.OpenSubKey("software\\test");
-
- // myreg = Key.OpenSubKey("software\\test",true);
-
- info = myreg.GetValue("test").ToString();
-
- myreg.Close();
info結果為:coolszy
3:刪除:
C#代碼 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=RegistryKey%20delKey%20%3D%20Registry.LocalMachine.OpenSubKey(%22Software%5C%5Ctest%22%2C%20true)%3B%0A%0AdelKey.DeleteValue(%22test%22)%3B%0A%0AdelKey.Close()%3B%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
- RegistryKey delKey = Registry.LocalMachine.OpenSubKey( "Software\\test" , true );
-
- delKey.DeleteValue("test" );
-
- delKey.Close();
C#代碼
- RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software\\test", true);
-
- delKey.DeleteValue("test");
-
- delKey.Close();
細心的讀者可能發現了第二個例子中OpenSubKey()方法參數與其他例子的不同。
如果你要修改鍵值,包括創建、設置、刪除鍵值等都要在方法後面加個布爾參數,設置為true,表示可寫可改;如果僅僅只是讀取鍵值可以不加,此時可寫關閉,你不能再往裡寫值(當然,你要加也可以true)!
還有讀者提到讀寫默認鍵值的問題,主要在設置、讀取的方法中將鍵名置空則就是對默認鍵值的操作。
如:
software.SetValue("", "coolszy"); // 在HKEY_LOCAL_MACHINE\SOFTWARE\test修改默認鍵值的值為“博coolszy”。讀取類似!
另外,默認的鍵值是不能刪除的,所以不要用DeleteValue()方法去刪除,會拋出異常的!
三:判斷注冊表項是否存在
C#代碼 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=private%20bool%20IsRegeditItemExist()%0A%0A%7B%0A%0A%20%20%20%20string%5B%5D%20subkeyNames%3B%0A%0A%20%20%20%20RegistryKey%20hkml%20%3D%20Registry.LocalMachine%3B%0A%0ARegistryKey%20software%20%3D%20hkml.OpenSubKey(%22SOFTWARE%22)%3B%0A%0A%2F%2FRegistryKey%20software%20%3D%20hkml.OpenSubKey(%22SOFTWARE%22%2C%20true)%3B%0A%0AsubkeyNames%20%3D%20software.GetSubKeyNames()%3B%20%0A%0A%2F%2F%E5%8F%96%E5%BE%97%E8%AF%A5%E9%A1%B9%E4%B8%8B%E6%89%80%E6%9C%89%E5%AD%90%E9%A1%B9%E7%9A%84%E5%90%8D%E7%A7%B0%E7%9A%84%E5%BA%8F%E5%88%97%EF%BC%8C%E5%B9%B6%E4%BC%A0%E9%80%92%E7%BB%99%E9%A2%84%E5%AE%9A%E7%9A%84%E6%95%B0%E7%BB%84%E4%B8%AD%0A%0A%20%20%20%20foreach%20(string%20keyName%20in%20subkeyNames)%20%20%20%2F%2F%E9%81%8D%E5%8E%86%E6%95%B4%E4%B8%AA%E6%95%B0%E7%BB%84%0A%0A%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%20if%20(keyName%20%3D%3D%20%22test%22)%20%2F%2F%E5%88%A4%E6%96%AD%E5%AD%90%E9%A1%B9%E7%9A%84%E5%90%8D%E7%A7%B0%0A%0A%20%20%20%20%20%20%20%20%20%7B%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20hkml.Close()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20hkml.Close()%3B%0A%0A%20%20%20%20%20%20%20%20%20return%20false%3B%20%20%20%20%20%20%20%20%20%20%20%20%0A%0A%7D%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
- private bool IsRegeditItemExist()
-
- {
-
- string [] subkeyNames;
-
- RegistryKey hkml = Registry.LocalMachine;
-
- RegistryKey software = hkml.OpenSubKey("SOFTWARE" );
-
- //RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
-
- subkeyNames = software.GetSubKeyNames();
-
- //取得該項下所有子項的名稱的序列,並傳遞給預定的數組中
-
- foreach ( string keyName in subkeyNames) //遍歷整個數組
-
- {
-
- if (keyName == "test" ) //判斷子項的名稱
-
- {
-
- hkml.Close();
-
- return true ;
-
- }
-
- }
-
- hkml.Close();
-
- return false ;
-
- }
C#代碼
- private bool IsRegeditItemExist()
-
- {
-
- string[] subkeyNames;
-
- RegistryKey hkml = Registry.LocalMachine;
-
- RegistryKey software = hkml.OpenSubKey("SOFTWARE");
-
- //RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
-
- subkeyNames = software.GetSubKeyNames();
-
- //取得該項下所有子項的名稱的序列,並傳遞給預定的數組中
-
- foreach (string keyName in subkeyNames) //遍歷整個數組
-
- {
-
- if (keyName == "test") //判斷子項的名稱
-
- {
-
- hkml.Close();
-
- return true;
-
- }
-
- }
-
- hkml.Close();
-
- return false;
-
- }
四:判斷鍵值是否存在
C#代碼 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=private%20bool%20IsRegeditKeyExit()%0A%0A%7B%0A%0A%20%20%20%20string%5B%5D%20subkeyNames%3B%0A%0A%20%20%20%20RegistryKey%20hkml%20%3D%20Registry.LocalMachine%3B%0A%0ARegistryKey%20software%20%3D%20hkml.OpenSubKey(%22SOFTWARE%5C%5Ctest%22)%3B%0A%0A%20%20%20%20%2F%2FRegistryKey%20software%20%3D%20hkml.OpenSubKey(%22SOFTWARE%5C%5Ctest%22%2C%20true)%3B%0A%0A%20%20%20%20subkeyNames%20%3D%20software.GetValueNames()%3B%0A%0A%2F%2F%E5%8F%96%E5%BE%97%E8%AF%A5%E9%A1%B9%E4%B8%8B%E6%89%80%E6%9C%89%E9%94%AE%E5%80%BC%E7%9A%84%E5%90%8D%E7%A7%B0%E7%9A%84%E5%BA%8F%E5%88%97%EF%BC%8C%E5%B9%B6%E4%BC%A0%E9%80%92%E7%BB%99%E9%A2%84%E5%AE%9A%E7%9A%84%E6%95%B0%E7%BB%84%E4%B8%AD%0A%0A%20%20%20%20foreach%20(string%20keyName%20in%20subkeyNames)%0A%0A%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%20if%20(keyName%20%3D%3D%20%22test%22)%20%20%20%2F%2F%E5%88%A4%E6%96%AD%E9%94%AE%E5%80%BC%E7%9A%84%E5%90%8D%E7%A7%B0%0A%0A%20%20%20%20%20%20%20%20%20%7B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20hkml.Close()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20true%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20hkml.Close()%3B%0A%0A%20%20%20%20%20%20%20%20%20return%20false%3B%0A%0A%20%20%20%20%7D%0A%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
- private bool IsRegeditKeyExit()
-
- {
-
- string [] subkeyNames;
-
- RegistryKey hkml = Registry.LocalMachine;
-
- RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test" );
-
- //RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test", true);
-
- subkeyNames = software.GetValueNames();
-
- //取得該項下所有鍵值的名稱的序列,並傳遞給預定的數組中
-
- foreach ( string keyName in subkeyNames)
-
- {
-
- if (keyName == "test" ) //判斷鍵值的名稱
-
- {
-
- hkml.Close();
-
- return true ;
-
- }
-
- }
-
- hkml.Close();
-
- return false ;
-
- }
C#代碼
- private bool IsRegeditKeyExit()
-
- {
-
- string[] subkeyNames;
-
- RegistryKey hkml = Registry.LocalMachine;
-
- RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test");
-
- //RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test", true);
-
- subkeyNames = software.GetValueNames();
-
- //取得該項下所有鍵值的名稱的序列,並傳遞給預定的數組中
-
- foreach (string keyName in subkeyNames)
-
- {
-
- if (keyName == "test") //判斷鍵值的名稱
-
- {
-
- hkml.Close();
-
- return true;
-
- }
-
- }
-
- hkml.Close();
-
- return false;
-
- }
至此,C#操作注冊表就到此為止了。本文幾乎囊括了C#語言對注冊表的所有操作,只要認真看完此文,認真時間就肯定對注冊表的讀取修改游刃有余了。
注:本文所有例子在VS2008+WinXP下調試通過…