注册表是Microsoft Windows操作系统和其应用程序中的一个重要的层次型数据库,用于存储系统和应用程序的设置信息。由键(key,或称“项”)、子键(subkey,子项)和值项(value)构成。一个键就是树状数据结构中的一个节点,而子键就是这个节点的子节点,子键也是键。一个值项则是一个键的一条属性,由名称(name)、数据类型(datatype)以及数据(data)组成。一个键可以有一个或多个值,每个值的名称各不相同,如果一个值的名称为空,则该值为该键的默认值。
(1)、注册表的数据类型主要有以下五种:
(2)、注册表有以下5个一级分支:
(3)、注册表的存储方式:
Windows NT系列操作系统和Windows 9x系列的存储方式有很大区别。注册表被分成多个文件存储,称为Registry Hives,每一个文件被称为一个配置单元。在早期的Windows 3.x系列中,注册表仅包含一个reg.dat文件,所存放的内容后来演变为HKEY_CLASSES_ROOT分支。
Windows NT家族的配置单元文件:
Windows 9x家族的配置单元文件:
二、C#操作注册表我们可以使用外部加载windows操作系统自带的 Advapi32.dll 文件,实现注册表的操作。下面实例中,我们使用 .NET 平台自带的 Microsoft.Win32.Registry 类库,使用的时候添加如下引用:
using Microsoft.Win32;
具体操作如下:
public static class RegistryUtil
{
#region 创建注册表
///
/// 创建注册表项
///
///
///
///
public static RegistryKey CreateRegistryKey(string keyPath, RegistryKey parentKey)
{
return parentKey?.CreateSubKey(keyPath);
}
///
/// 创建注册表项目
///
///
///
public static RegistryKey CreateRegistryLocalMachine64Key(string keyPath)
{
return Registry.LocalMachine.CreateSubKey(keyPath);
}
///
/// 创建注册表项目
///
///
///
public static RegistryKey CreateRegistryClassesRootKey(string keyPath)
{
return Registry.ClassesRoot.CreateSubKey(keyPath);
}
///
/// 创建注册表项目
///
///
///
public static RegistryKey CreateRegistryCurrentConfigKey(string keyPath)
{
return Registry.CurrentConfig.CreateSubKey(keyPath);
}
///
/// 创建注册表项目
///
///
///
public static RegistryKey CreateRegistryCurrentUserKey(string keyPath)
{
return Registry.CurrentUser.CreateSubKey(keyPath);
}
///
/// 创建注册表项目
///
///
///
public static RegistryKey CreateRegistryPerformanceDataKey(string keyPath)
{
return Registry.PerformanceData.CreateSubKey(keyPath);
}
///
/// 创建注册表项目
///
///
///
public static RegistryKey CreateRegistryUsersKey(string keyPath)
{
return Registry.Users.CreateSubKey(keyPath);
}
#endregion
#region 打开注册表
///
/// 打开 的
///
///
public static RegistryKey OpenLocalMachine64Key()
{
return RegistryKey.OpenbaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64);
}
///
/// 打开 的
///
///
public static RegistryKey OpenLocalMachine32Key()
{
return RegistryKey.OpenbaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
}
///
/// 打开 的
///
///
public static RegistryKey OpenCurrentUser64Key()
{
return RegistryKey.OpenbaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry64);
}
///
/// 打开 的
///
///
public static RegistryKey OpenCurrentUser32Key()
{
return RegistryKey.OpenbaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry32);
}
///
/// 打开注册表键
///
///
///
///
public static RegistryKey OpenKey(this RegistryKey rootKey, string keyPath)
{
return rootKey.OpenSubKey(keyPath, true);
}
#endregion
///
/// 读取注册表值
///
///
///
///
public static string ReadRegistryValue(this RegistryKey key, string name)
{
return key?.GetValue(name)?.ToString();
}
///
/// 写入注册表值
///
///
///
///
public static void WriteRegistryValue(this RegistryKey key, string name, string value)
{
key.SetValue(name, value);
}
///
/// 删除注册值
///
///
///
public static void DeleteRegistryValue(this RegistryKey key, string name)
{
key.Deletevalue(name);
}
}
调试代码如下:
static void Main(string[] args)
{
// 写入注册表
string path = @"SoftwareTestChar";
var key = RegistryUtil.CreateRegistryLocalMachine64Key(path);
key.WriteRegistryValue("Name", "Dwayne");
key.WriteRegistryValue("Age", "18");
// 读取注册表
var regKey = RegistryUtil.OpenLocalMachine64Key().OpenKey(path);
var name = regKey.ReadRegistryValue("Name");
var age = regKey.ReadRegistryValue("Age");
Console.WriteLine($"Name={name},Age={age}");
Console.WriteLine("Hello World!");
}
以上就是如何在C# 中使用注册表的详细内容,更多关于c# 使用注册表的资料请关注考高分网其它相关文章!



