注册表的操作(Registry handling with .NET)
2012-02-01 18:03
267 查看
Introduction
Well, apparently the registry seems to have lost some of its importance with the arrival of .NET, at least that's the impression I seem to get. But luckily for us, Microsoft has given us two niceclasses for doing just about anything we want to do with the registry. The classes are
Microsoft.Win32.RegistryKeyand
Microsoft.Win32.Registry.
They have both been put into the
Microsoft.Win32namespace as you can see because the registry is totally Microsoft Win32 specific. Without too much fuss,
let's get into business and try and do some of the stuff we normally do with the registry.
Reading the registry
//The Registry class provides us with the // registry root keys RegistryKey rkey = Registry.LocalMachine; //Now let's open one of the sub keys RegistryKey rkey1=rkey.OpenSubKey( "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"); //Now using GetValue(...) we read in various values //from the opened key listBox1.Items.Add("RegisteredOwner :- " + rkey1.GetValue("RegisteredOwner")); listBox1.Items.Add("RegisteredOrganization :- " + rkey1.GetValue("RegisteredOrganization")); listBox1.Items.Add("ProductName :- " + rkey1.GetValue("ProductName")); listBox1.Items.Add("CSDVersion :- " + rkey1.GetValue("CSDVersion")); listBox1.Items.Add("SystemRoot :- " + rkey1.GetValue("SystemRoot")); rkey1.Close();
Writing to the registry
rkey = Registry.CurrentUser; //The second parameter tells it to open the key as writable rkey1 = rkey.OpenSubKey("Software",true); // Now we create our sub key [assuming you have enough // rights to edit this area of the registry] RegistryKey rkey2 = rkey1.CreateSubKey("Tweety"); //Setting the various values is done using SetValue() //I couldn't figure out how to set the value type yet :-( rkey2.SetValue("Name","Tweety"); rkey2.SetValue("Age",24); rkey2.Close(); rkey1.Close();
If you open regedit, you'll see that the new key has been added and the values have indeed been written correctly.
Enumeration
Okay, we've read from and written into the registry. Now let's enumerate some values.rkey1 = rkey.OpenSubKey("Software\\Microsoft\\" + "Internet Account Manager\\Accounts\\00000001"); string[] s_arr = rkey1.GetValueNames(); foreach(String s in s_arr) { listBox1.Items.Add(s + " :- " + rkey1.GetValue(s)); } rkey1.Close();
Well, that's about it I guess. This was originally written as part of an internal tutorial. I didn't modify it too much except for taking better screenshots.
Thanks.
相关文章推荐
- c#.net操作注册表RegistryKey
- VB.NET 轻松操作注册表
- [原创]C#.Net 下的注册表操作(含代码)
- VB.NET轻松操作注册表
- C#.NET对注册表的常用操作(增加项,判断指定项是否存在,增加键(键的名称,键的数值),判断指定的键是否存在)
- 用KOL操作注册表(非DELPHI的registry)
- 注册表的基本操作(.Net)
- .Net WinForm创建系统托盘以及操作注册表
- Operating the registry help class(C#读写注册表操作)
- vb.net下对注册表的各种操作!(谢neu2000指路:))
- VB.Net:注册表的简单操作
- net精华:C#中对注册表的操作
- vb.net操作注册表
- vb.net操作注册表的方法分析【增加,修改,删除,查询】
- c# RegistryKey注册表操作
- Exception handling with custom error pages in ASP.NET using C#.
- ASP.NET中如何操作注册表
- VB.NET 操作注册表
- net 操作Windows注册表
- [转]RegistryKey 操作注册表