相信每个人对注册表并不陌生,在运行里面输入“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;
在这个命名空间里面包含了许多注册表相关的类,足够我们使用了~~
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" );
-
- RegistryKey key = Registry.LocalMachine;
-
- RegistryKey software = key.CreateSubKey("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 );
-
- RegistryKey key = Registry.LocalMachine;
-
- RegistryKey software = key.OpenSubKey("software\\test",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();
- 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" );
- 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" );
-
-
- info = myreg.GetValue("test" ).ToString();
-
- myreg.Close();
- string info = "";
-
- RegistryKey Key;
-
- Key = Registry.LocalMachine;
-
- myreg = Key.OpenSubKey("software\\test");
-
-
- 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();
- 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" );
-
-
- subkeyNames = software.GetSubKeyNames();
-
-
- foreach ( string keyName in subkeyNames)
-
- {
-
- if (keyName == "test" )
-
- {
-
- hkml.Close();
-
- return true ;
-
- }
-
- }
-
- hkml.Close();
-
- return false ;
-
- }
- private bool IsRegeditItemExist()
-
- {
-
- string[] subkeyNames;
-
- RegistryKey hkml = Registry.LocalMachine;
-
- RegistryKey software = hkml.OpenSubKey("SOFTWARE");
-
-
- 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" );
-
-
-
- subkeyNames = software.GetValueNames();
-
-
- foreach ( string keyName in subkeyNames)
-
- {
-
- if (keyName == "test" )
-
- {
-
- hkml.Close();
-
- return true ;
-
- }
-
- }
-
- hkml.Close();
-
- return false ;
-
- }
- private bool IsRegeditKeyExit()
-
- {
-
- string[] subkeyNames;
-
- RegistryKey hkml = Registry.LocalMachine;
-
- RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test");
-
-
-
- subkeyNames = software.GetValueNames();
-
-
- foreach (string keyName in subkeyNames)
-
- {
-
- if (keyName == "test")
-
- {
-
- hkml.Close();
-
- return true;
-
- }
-
- }
-
- hkml.Close();
-
- return false;
-
- }
至此,C#操作注册表就到此为止了。本文几乎囊括了C#语言对注册表的所有操作,只要认真看完此文,认真时间就肯定对注册表的读取修改游刃有余了。
注:本文所有例子在VS2008+WinXP下调试通过…