C#操作注册表全攻略

相信每个人对注册表并不陌生,在运行里面输入“regedit”就可以打开注册表编辑器了。这东西对Windows系统来说可是比较重要的,也是病 毒常常会光顾的地方,比如病毒和恶意软件常常会在注册表的启动项里面写入自己的启动键值来达到自启动的目的,有些病毒还会修改注册表里面来映像劫持杀毒软 件,这是破坏系统的第一步。同时,大多软件(软件的序列号和信息)和硬件信息、系统信息、安全模式等等设置都保存在这里,因此系统的健康在很大程度上要依 赖注册表的健康。
       作为编程开发人员,我们有必要了解注册表并学会操作注册表。下面我们就来用.NET下托管语言C#操作注册表,主要内容包括:注册表项的创建,打开与删除、键值的创建(设置值、修改),读取和删除、判断注册表项是否存在、判断键值是否存在。 
准备工作:
1:要操作注册表,我们必须要引入必要的命名空间:

  1. using  Microsoft.Win32;
  1. using Microsoft.Win32;

在这个命名空间里面包含了许多注册表相关的类,足够我们使用了~~
2:命名空间里面提供了一个类:RegistryKey   利用它我们可以定位到注册表最开头的分支:
ClassesRoot,CurrentUser,Users,LocalMachine,CurrentConfig
如:RegistryKey key = Registry.LocalMachine;
3:在操作的过程中涉及到子分支,要用\\进行深入,单个\会报错!
4:最后要调用RegistryKey对象的Close()关闭对注册表的修改~~~
5:以下我们的例子都是在LocalMachine分支下,请注意。

一:注册表项的创建,打开与删除
1:创建:
创建注册表项主要用到RegistryKey 的CreateSubKey()方法。如:

  1. RegistryKey key = Registry.LocalMachine;
  2. RegistryKey software = key.CreateSubKey("software\\test" );
  3. //在HKEY_LOCAL_MACHINE\SOFTWARE下新建名为test的注册表项。如果已经存在则不影响!
  1. RegistryKey key = Registry.LocalMachine;
  2. RegistryKey software = key.CreateSubKey("software\\test");
  3. //在HKEY_LOCAL_MACHINE\SOFTWARE下新建名为test的注册表项。如果已经存在则不影响!

2:打开:
打开注册表项主要用到RegistryKey 的OpenSubKey()方法。如:

  1. RegistryKey key = Registry.LocalMachine;
  2. RegistryKey software = key.OpenSubKey("software\\test" , true );
  3. //注意该方法后面还可以有一个布尔型的参数,true表示可以写入。
  1. RegistryKey key = Registry.LocalMachine;
  2. RegistryKey software = key.OpenSubKey("software\\test",true);
  3. //注意该方法后面还可以有一个布尔型的参数,true表示可以写入。

注意,如果该注册表项不存在,这调用这个方法会抛出异常

3:删除:
删除注册表项主要用到RegistryKey 的DeleteSubKey()方法。如:

  1. RegistryKey key = Registry.LocalMachine;
  2. key.DeleteSubKey("software\\test" , true );     //该方法无返回值,直接调用即可
  3. key.Close();
  1. RegistryKey key = Registry.LocalMachine;
  2. key.DeleteSubKey("software\\test",true);    //该方法无返回值,直接调用即可
  3. key.Close();

注意,如果该注册表项不存在,这调用这个方法会抛出异常

二:键值的创建(设置值、修改),读取和删除
1:创建(设置值、修改):
对键值的创建修改等操作主要用到RegistryKey 的SetValue()方法

  1. RegistryKey key = Registry.LocalMachine;
  2. RegistryKey software = key.OpenSubKey("software\\test" , true );  //该项必须已存在
  3. software.SetValue("test" ,  "coolszy" );
  1. RegistryKey key = Registry.LocalMachine;
  2. RegistryKey software = key.OpenSubKey("software\\test",true); //该项必须已存在
  3. software.SetValue("test", "coolszy");

//在HKEY_LOCAL_MACHINE\SOFTWARE\test下创建一个名为“test”,值为“coolszy”的键值。如果该键值原本已经存在,则会修改替换原来的键值,如果不存在则是创建该键值。
// 注意:SetValue()还有第三个参数,主要是用于设置键值的类型,如:字符串,二进制,Dword等等~~默认是字符串。如:
// software.SetValue("test", "0", RegistryValueKind.DWord); //二进制信息
Key.Close();

2:读取:

  1. string  info =  "" ;
  2. RegistryKey Key;
  3. Key = Registry.LocalMachine;
  4. myreg = Key.OpenSubKey("software\\test" );
  5. // myreg = Key.OpenSubKey("software\\test",true);
  6. info = myreg.GetValue("test" ).ToString();
  7. myreg.Close();
  1. string info = "";
  2. RegistryKey Key;
  3. Key = Registry.LocalMachine;
  4. myreg = Key.OpenSubKey("software\\test");
  5. // myreg = Key.OpenSubKey("software\\test",true);
  6. info = myreg.GetValue("test").ToString();
  7. myreg.Close();

info结果为:coolszy
3:删除:

  1. RegistryKey delKey = Registry.LocalMachine.OpenSubKey( "Software\\test" ,  true );
  2. delKey.DeleteValue("test" );
  3. delKey.Close();
  1. RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software\\test", true);
  2. delKey.DeleteValue("test");
  3. delKey.Close();

细心的读者可能发现了第二个例子中OpenSubKey()方法参数与其他例子的不同。
如果你要修改键值,包括创建、设置、删除键值等都要在方法后面加个布尔参数,设置为true,表示可写可改;如果仅仅只是读取键值可以不加,此时可写关闭,你不能再往里写值(当然,你要加也可以true)!
还有读者提到读写默认键值的问题,主要在设置、读取的方法中将键名置空则就是对默认键值的操作。
如:
software.SetValue("", "coolszy"); //   在HKEY_LOCAL_MACHINE\SOFTWARE\test修改默认键值的值为“博coolszy”。读取类似!
另外,默认的键值是不能删除的,所以不要用DeleteValue()方法去删除,会抛出异常的!

三:判断注册表项是否存在

  1. private   bool  IsRegeditItemExist()
  2. {
  3. string [] subkeyNames;
  4. RegistryKey hkml = Registry.LocalMachine;
  5. RegistryKey software = hkml.OpenSubKey("SOFTWARE" );
  6. //RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
  7. subkeyNames = software.GetSubKeyNames();
  8. //取得该项下所有子项的名称的序列,并传递给预定的数组中
  9. foreach  ( string  keyName  in  subkeyNames)    //遍历整个数组
  10. {
  11. if  (keyName ==  "test" )  //判断子项的名称
  12. {
  13. hkml.Close();
  14. return   true ;
  15. }
  16. }
  17. hkml.Close();
  18. return   false ;
  19. }
  1. private bool IsRegeditItemExist()
  2. {
  3. string[] subkeyNames;
  4. RegistryKey hkml = Registry.LocalMachine;
  5. RegistryKey software = hkml.OpenSubKey("SOFTWARE");
  6. //RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
  7. subkeyNames = software.GetSubKeyNames();
  8. //取得该项下所有子项的名称的序列,并传递给预定的数组中
  9. foreach (string keyName in subkeyNames)   //遍历整个数组
  10. {
  11. if (keyName == "test") //判断子项的名称
  12. {
  13. hkml.Close();
  14. return true;
  15. }
  16. }
  17. hkml.Close();
  18. return false;
  19. }

四:判断键值是否存在

  1. private   bool  IsRegeditKeyExit()
  2. {
  3. string [] subkeyNames;
  4. RegistryKey hkml = Registry.LocalMachine;
  5. RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test" );
  6. //RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test", true);
  7. subkeyNames = software.GetValueNames();
  8. //取得该项下所有键值的名称的序列,并传递给预定的数组中
  9. foreach  ( string  keyName  in  subkeyNames)
  10. {
  11. if  (keyName ==  "test" )    //判断键值的名称
  12. {
  13. hkml.Close();
  14. return   true ;
  15. }
  16. }
  17. hkml.Close();
  18. return   false ;
  19. }
  1. private bool IsRegeditKeyExit()
  2. {
  3. string[] subkeyNames;
  4. RegistryKey hkml = Registry.LocalMachine;
  5. RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test");
  6. //RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test", true);
  7. subkeyNames = software.GetValueNames();
  8. //取得该项下所有键值的名称的序列,并传递给预定的数组中
  9. foreach (string keyName in subkeyNames)
  10. {
  11. if (keyName == "test")   //判断键值的名称
  12. {
  13. hkml.Close();
  14. return true;
  15. }
  16. }
  17. hkml.Close();
  18. return false;
  19. }

至此,C#操作注册表就到此为止了。本文几乎囊括了C#语言对注册表的所有操作,只要认真看完此文,认真时间就肯定对注册表的读取修改游刃有余了。
注:本文所有例子在VS2008+WinXP下调试通过…

上一篇:(10)Python函数


下一篇:从NLP中的标记算法(tokenization)到bert中的WordPiece