Working with Windows Registry 使用windows注册表 C#
Windows注册表是应用程序配置设置和应用程序所需的其他信息的中央数据库。实际上,除了读取和写入数据之外,你不能对Windows注册表做任何其他事情;在这篇短文中,我将展示如何读、写和删除windows注册表。
如果你从来没有用过windows注册表,你可以在命令行中运行regedit,就可以看到了;

点击OK按钮就可以打开注册表编辑器。如下所示,注册表是用于各种设置的多层次数据存储。在我的电脑中主要包含5大部分。

.NET框架类库提供了两大类---Registry和RegistryKey用于处理注册表。这些类定义在Microsoft.Win32命名空间中。所以,在使用这些类之前,需要添加对这个命名空间的引用。
Registry类
Registry类包含提供对注册表项进行访问的成员。我们可以依据下列规则定义注册表项:
CurrentUser---存储有关用户首选项的信息。
LocalMachine---存储本地计算机的配置信息.
ClassesRoot---存储有关类型(和类)及其属性的信息.
User---存储有关默认用户配置的信息。
PerformanceData---存储软件组件的性能信息。
CurrentConfig---存储非用户特定的硬件信息。
DynData---存储动态数据。
registry类有一个对应于这些键类型的相关字段。Registry类成员如下表所述:
(具体内容可参考:https://learn.microsoft.com/zh-cn/dotnet/api/microsoft.win32.registry?redirectedfrom=MSDN&view=net-7.0)
| ClassesRoot |
定义文档的类型(或类)以及与那些类型关联的属性。 该字段读取 Windows 注册表基项 HKEY_CLASSES_ROOT。 |
| CurrentConfig |
包含有关非用户特定的硬件的配置信息。 该字段读取 Windows 注册表基项 HKEY_CURRENT_CONFIG。 |
| CurrentUser |
包含有关当前用户首选项的信息。 此字段读取 Windows 注册表基项 HKEY_CURRENT_USER。 |
| LocalMachine |
包含本地计算机的配置数据。 该字段读取 Windows 注册表基项 HKEY_LOCAL_MACHINE。 |
| PerformanceData |
包含软件组件的性能信息。 该字段读取 Windows 注册表基项 HKEY_PERFORMANCE_DATA。 |
| Users |
包含有关默认用户配置的信息。 该字段读取 Windows 注册表基项 HKEY_USERS。 |
比如说,如果你想要访问HKEY_LOCAL_MACHINE项,你需要调用Registry.LocalMachine,它返回一个RegistryKey类型:
RegistryKey pRegKey = Registry.LocalMachine;
RegistryKey类
RegistryKey类包含的成员用于添加、移除、替换和读取注册表数据。相关方法和属性如下表所示:
属性
| Handle |
获取一个 SafeRegistryHandle 对象,该对象表示当前 RegistryKey 对象封装的注册表项。 |
| Name |
检索项的名称。 |
| SubKeyCount |
检索当前项的子项计数。 |
| ValueCount |
检索项中值的计数。 |
| View |
获取用于创建注册表项的视图。 |
方法
| Close() |
关闭该项,如果其内容已修改,则将其刷新到磁盘。 |
| CreateSubKey(String) |
创建一个新子项或打开一个现有子项以进行写访问。 |
| DeleteSubKey(String) |
删除指定子项。 |
| DeleteSubKey(String, Boolean) |
删除指定的子项,并指定在找不到该子项时是否引发异常。 |
| DeleteSubKeyTree(String) | 递归删除子项和任何子级子项。 |
| DeleteValue(String) | 从此项中删除指定值。 |
| GetSubKeyNames() | 检索包含所有子项名称的字符串数组。 |
| GetValue(String) | 检索与指定名称关联的值。 如果注册表中不存在名称/值对,则返回 null。 |
| GetValueNames() | 检索包含与此项关联的所有值名称的字符串数组。 |
| OpenSubKey(String) | 以只读方式检索子项。 |
| OpenSubKey(String, Boolean) | 检索指定的子项,并指定是否将写访问权限应用于该项。 |
| SetValue(String, Object) | 设置指定的名称/值对。 |
Methods
| Method | Description |
| Close | Closes the key. |
| CreateSubKey | Creates a new subkey if not exists, otherwise opens an existing subkey. |
| DeleteSubKey | Deletes the specified subkey. |
| DeleteSubKeyTree | Deletes a subkey and any children. |
| DeleteValue | Deletes the specified value from a key. |
| GetSubKeyNames | Returns an array of strings that contains all the subkey names. |
| GetValue | Returns the specified value. |
| GetValueNames | Retrieves an array of strings that contains all the value names associated with this key. |
| OpenSubKey | Opens a subkey. |
| SetValue | Sets the specified value. |
注:个人感觉上面 的英文解释的更通俗一些;
向注册表添加项和值
在接下来的简单示例中,让我们看一下如何使用这些方法来添加、删除和升级项及它们的值。
我们添加一个项MCBInc,添加之后,注册表如下图所示

我们用CreateSubKey来添加一个新项到注册表中,并调用SetValue方法写入值和项,下面的代码就是实现的代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 using Microsoft.Win32; 8 9 namespace CreateSubKeyDemo 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true); 16 RegistryKey newkey = key.CreateSubKey("MCBInc"); 17 newkey.SetValue("MCBInc", "NET Developer"); 18 key.Close(); 19 } 20 } 21 }

备注:为什么注册项写在HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node下面,可参照https://blog.csdn.net/weixin_43145361/article/details/87982245
接下来再举例来展示如何创建项及值,示例二
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 using Microsoft.Win32; 8 9 namespace CreateSubKeyDemo 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true); 16 //RegistryKey newkey = key.CreateSubKey("MCBInc"); 17 //newkey.SetValue("MCBInc", "NET Developer"); 18 //key.Close(); 19 20 //示例二 21 string regpath = @"Software\ChenSoft\Soft1"; 22 RegistryKey key = Registry.LocalMachine.CreateSubKey(regpath); 23 key.SetValue("zhangynmei", "mei"); 24 key.Close(); 25 26 //示例三 27 //string regchen = "Software\\TongSoft\\Soft2"; 28 //RegistryKey key2 = Registry.LocalMachine.CreateSubKey(regchen); 29 //key2.SetValue("Tong", "chen"); 30 //key2.Close(); 31 32 //示例四 33 //Microsoft.Win32.RegistryKey key; 34 //key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Names"); 35 //key.SetValue("Name", "Isabella"); 36 //key.Close(); 37 } 38 } 39 }

示例二创建的注册表项也是在wow6432Node目录下;
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 using Microsoft.Win32; 8 9 namespace CreateSubKeyDemo 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true); 16 //RegistryKey newkey = key.CreateSubKey("MCBInc"); 17 //newkey.SetValue("MCBInc", "NET Developer"); 18 //key.Close(); 19 20 //示例二 21 //string regpath = @"Software\ChenSoft\Soft1"; 22 //RegistryKey key = Registry.LocalMachine.CreateSubKey(regpath); 23 //key.SetValue("zhangynmei", "mei"); 24 //key.Close(); 25 26 //示例三 27 string regchen = "Software\\TongSoft\\Soft2"; 28 RegistryKey key2 = Registry.LocalMachine.CreateSubKey(regchen); 29 key2.SetValue("Tong", "chen"); 30 key2.Close(); 31 32 //示例四 33 //Microsoft.Win32.RegistryKey key; 34 //key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Names"); 35 //key.SetValue("Name", "Isabella"); 36 //key.Close(); 37 } 38 } 39 }

以上是示例三,创建的注册表项也是在wow6432Node目录下;
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 using Microsoft.Win32; 8 9 namespace CreateSubKeyDemo 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true); 16 //RegistryKey newkey = key.CreateSubKey("MCBInc"); 17 //newkey.SetValue("MCBInc", "NET Developer"); 18 //key.Close(); 19 20 //示例二 21 //string regpath = @"Software\ChenSoft\Soft1"; 22 //RegistryKey key = Registry.LocalMachine.CreateSubKey(regpath); 23 //key.SetValue("zhangynmei", "mei"); 24 //key.Close(); 25 26 //示例三 27 //string regchen = "Software\\TongSoft\\Soft2"; 28 //RegistryKey key2 = Registry.LocalMachine.CreateSubKey(regchen); 29 //key2.SetValue("Tong", "chen"); 30 //key2.Close(); 31 32 //示例四 33 Microsoft.Win32.RegistryKey key; 34 key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Names"); 35 key.SetValue("Name", "Isabella"); 36 key.Close(); 37 } 38 } 39 }

以上是示例四,增加的注册表项是在CurrentUser目录下;
Retrieving Data from the Registry在注册表中检索数据
在接下来的短文中,我们学习如何用这些方法来添加、移除和升级项和值。
GetValue方法以Object的形式返回子键的值。在下面的示例中,我读取CenteralProcessor的值/0子项并写入console。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 using Microsoft.Win32; 8 9 namespace CreateSubKeyDemo 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true); 16 //RegistryKey newkey = key.CreateSubKey("MCBInc"); 17 //newkey.SetValue("MCBInc", "NET Developer"); 18 //key.Close(); 19 20 //示例二 21 //string regpath = @"Software\ChenSoft\Soft1"; 22 //RegistryKey key = Registry.LocalMachine.CreateSubKey(regpath); 23 //key.SetValue("zhangynmei", "mei"); 24 //key.Close(); 25 26 //示例三 27 //string regchen = "Software\\TongSoft\\Soft2"; 28 //RegistryKey key2 = Registry.LocalMachine.CreateSubKey(regchen); 29 //key2.SetValue("Tong", "chen"); 30 //key2.Close(); 31 32 //示例四 33 //Microsoft.Win32.RegistryKey key; 34 //key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Names"); 35 //key.SetValue("Name", "Isabella"); 36 //key.Close(); 37 38 //示例五 39 RegistryKey pRegKey = Registry.LocalMachine; 40 pRegKey = pRegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"); 41 object val = pRegKey.GetValue("VendorIdentifier"); 42 Console.WriteLine("The central processor of this machine is:" + val); 43 } 44 } 45 }


删除数据
DeleteValue方法用于删除子项的值。DeleteSubKey删除定义的子项。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Drawing; 6 using System.Windows.Forms; 7 using Microsoft.Win32; 8 9 namespace CreateSubKeyDemo 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true); 16 //RegistryKey newkey = key.CreateSubKey("MCBInc"); 17 //newkey.SetValue("MCBInc", "NET Developer"); 18 //key.Close(); 19 20 //示例二 21 //string regpath = @"Software\ChenSoft\Soft1"; 22 //RegistryKey key = Registry.LocalMachine.CreateSubKey(regpath); 23 //key.SetValue("zhangynmei", "mei"); 24 //key.Close(); 25 26 //示例三 27 //string regchen = "Software\\TongSoft\\Soft2"; 28 //RegistryKey key2 = Registry.LocalMachine.CreateSubKey(regchen); 29 //key2.SetValue("Tong", "chen"); 30 //key2.Close(); 31 32 //示例四 33 //Microsoft.Win32.RegistryKey key; 34 //key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Names"); 35 //key.SetValue("Name", "Isabella"); 36 //key.Close(); 37 38 //示例五 39 //RegistryKey pRegKey = Registry.LocalMachine; 40 //pRegKey = pRegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"); 41 //object val = pRegKey.GetValue("VendorIdentifier"); 42 //Console.WriteLine("The central processor of this machine is:" + val); 43 44 //示例六 45 RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software", true); 46 delKey.DeleteSubKey("MCBInc"); 47 } 48 } 49 }
示例六,chenlight测试成功,是可以删除子项的;
---------------------------------------------------------------------------------------------------------------------------------
https://www.c-sharpcorner.com/article/working-with-windows-registry/#:~:text=Working%20with%20Windows%20Registry%201%20The%20Registry%20Class,...%204%20Retrieving%20Data%20from%20the%20Registry%20

浙公网安备 33010602011771号