在Windows系统开发中,注册表是存储系统和应用程序配置信息的重要数据库,C#提供了专门的类库支持对注册表的操作,开发者可以通过对应的API完成键值的读取、修改、创建和删除等操作。

C#操作注册表的核心类
C#操作注册表主要依赖Microsoft.Win32命名空间下的Registry类和RegistryKey类。Registry类提供了访问注册表根键的静态属性,RegistryKey类则用于操作具体的注册表项和键值。
常见的注册表根键对应Registry类的静态属性如下:
- Registry.ClassesRoot:对应HKEY_CLASSES_ROOT,存储文件类型和COM组件信息
- Registry.CurrentUser:对应HKEY_CURRENT_USER,存储当前用户的配置信息
- Registry.LocalMachine:对应HKEY_LOCAL_MACHINE,存储本地计算机的全局配置信息
- Registry.Users:对应HKEY_USERS,存储所有用户的配置信息
- Registry.CurrentConfig:对应HKEY_CURRENT_CONFIG,存储当前的硬件配置信息
读取注册表键值
读取注册表键值需要先获取对应的注册表项,再通过GetValue方法获取指定键的值。如果注册表项不存在,需要先判断再操作,避免出现异常。
读取示例:获取当前用户的桌面壁纸路径
桌面壁纸路径存储在HKEY_CURRENT_USERControl PanelDesktop下的WallPaper键值中,以下是读取该键值的代码示例:
using Microsoft.Win32;
using System;
namespace RegistryDemo
{
class Program
{
static void Main(string[] args)
{
// 获取当前用户根键
RegistryKey currentUserKey = Registry.CurrentUser;
// 打开目标注册表项,第二个参数false表示以只读方式打开
RegistryKey desktopKey = currentUserKey.OpenSubKey(@"Control PanelDesktop", false);
if (desktopKey != null)
{
// 读取WallPaper键值,第二个参数是默认值,当键值不存在时返回该值
object wallpaperPath = desktopKey.GetValue("WallPaper", "未找到壁纸路径");
Console.WriteLine($"当前桌面壁纸路径:{wallpaperPath}");
// 关闭注册表项,释放资源
desktopKey.Close();
}
else
{
Console.WriteLine("未找到对应的注册表项");
}
currentUserKey.Close();
}
}
}
修改注册表键值
修改注册表键值需要先以可写权限打开注册表项,再使用SetValue方法设置键值。如果键值不存在,会自动创建该键值;如果注册表项不存在,需要先创建注册表项。
修改示例:修改当前用户的鼠标悬停时间
鼠标悬停时间存储在HKEY_CURRENT_USERControl PanelMouse下的MouseHoverTime键值中,单位为毫秒,以下是修改该键值的代码示例:
using Microsoft.Win32;
using System;
namespace RegistryDemo
{
class Program
{
static void Main(string[] args)
{
RegistryKey currentUserKey = Registry.CurrentUser;
// 打开注册表项,第二个参数true表示以可写权限打开
RegistryKey mouseKey = currentUserKey.OpenSubKey(@"Control PanelMouse", true);
if (mouseKey != null)
{
// 设置MouseHoverTime键值为500毫秒
mouseKey.SetValue("MouseHoverTime", 500);
Console.WriteLine("鼠标悬停时间已修改为500毫秒");
// 验证修改结果
object newValue = mouseKey.GetValue("MouseHoverTime");
Console.WriteLine($"修改后的鼠标悬停时间:{newValue}");
mouseKey.Close();
}
else
{
Console.WriteLine("未找到对应的注册表项");
}
currentUserKey.Close();
}
}
}
创建和删除注册表项与键值
创建注册表项和键值
使用CreateSubKey方法可以创建新的注册表项,如果项已存在则直接打开该注册表项。创建后可以通过SetValue方法添加键值。
using Microsoft.Win32;
using System;
namespace RegistryDemo
{
class Program
{
static void Main(string[] args)
{
RegistryKey localMachineKey = Registry.LocalMachine;
// 创建新的注册表项,路径为SOFTWAREMyAppConfig
RegistryKey myAppKey = localMachineKey.CreateSubKey(@"SOFTWAREMyAppConfig");
if (myAppKey != null)
{
// 添加两个键值
myAppKey.SetValue("AppName", "我的应用程序");
myAppKey.SetValue("Version", "1.0.0");
myAppKey.SetValue("AutoStart", 1, RegistryValueKind.DWord);
Console.WriteLine("注册表项和键值创建成功");
myAppKey.Close();
}
localMachineKey.Close();
}
}
}
删除注册表项和键值
删除键值使用DeleteValue方法,删除注册表项使用DeleteSubKey或DeleteSubKeyTree方法,其中DeleteSubKeyTree会删除项及其所有子项和键值。
using Microsoft.Win32;
using System;
namespace RegistryDemo
{
class Program
{
static void Main(string[] args)
{
RegistryKey localMachineKey = Registry.LocalMachine;
RegistryKey myAppKey = localMachineKey.OpenSubKey(@"SOFTWAREMyAppConfig", true);
if (myAppKey != null)
{
// 删除AutoStart键值
myAppKey.DeleteValue("AutoStart", false);
Console.WriteLine("AutoStart键值已删除");
myAppKey.Close();
}
// 删除整个MyAppConfig注册表项及其子内容
localMachineKey.DeleteSubKeyTree(@"SOFTWAREMyAppConfig", false);
Console.WriteLine("MyAppConfig注册表项已删除");
localMachineKey.Close();
}
}
}
操作注册表的注意事项
- 操作注册表需要对应的权限,修改HKEY_LOCAL_MACHINE下的项通常需要管理员权限,否则会抛出未授权异常
- 操作完注册表项后要及时调用
Close方法关闭,避免资源泄露 - 读取键值时要做好空值判断,避免强制转换导致的类型异常
- 修改系统关键注册表项前建议先备份,避免误操作导致系统异常
- 如果程序是32位运行在64位系统上,访问HKEY_LOCAL_MACHINESOFTWARE下的项会被重定向到HKEY_LOCAL_MACHINESOFTWAREWow6432Node,需要根据实际情况处理路径
注册表操作属于敏感操作,非必要场景不建议频繁修改系统注册表,应用程序的配置信息优先选择配置文件或应用数据目录存储。
C#注册表操作Windows注册表Registry类修改时间:2026-06-19 19:42:36