写在前面
开发Windows桌面程序时有时候需要操作注册表,很有必要来个工具类;
访问注册表的路径示例: @"SYSTEM\CurrentControlSet\Services" 需要对 \ 做转义
需要引用:using Microsoft.Win32;
工具类代码如下:
public class RegistryHelper
{
public static RegistryKey GetRegistryKey(RegistryHive registryHive = RegistryHive.LocalMachine)
{
var registryKey = RegistryKey.OpenBaseKey(registryHive,Environment.Is64BitOperatingSystem
? RegistryView.Registry64
: RegistryView.Registry32);
return registryKey;
}
public static RegistryKey GetRegistryKey(string keyPath,RegistryHive registryHive = RegistryHive.LocalMachine)
{
var registryKey = RegistryKey.OpenBaseKey(registryHive,Environment.Is64BitOperatingSystem
? RegistryView.Registry64
: RegistryView.Registry32);
if (registryKey == null) return null;
return string.IsNullOrEmpty(keyPath) ? registryKey : registryKey.OpenSubKey(keyPath,true); // writable 设置为true 才能写入
}
public static void CreateKey(string parentPath,string key,object value,RegistryValueKind kind = RegistryValueKind.String,RegistryHive registryHive = RegistryHive.LocalMachine)
{
var parentKey = GetRegistryKey(parentPath,registryHive);
if (parentKey == null) return;
parentKey.SetValue(key,value,kind);
parentKey.Close();
}
public static void SetValue(string keyPath,RegistryHive registryHive = RegistryHive.LocalMachine)
{
var registryKey = GetRegistryKey(keyPath,registryHive);
if (registryKey == null) return;
registryKey.SetValue(keyPath,value);
}
public static object GetValue(string keyPath,RegistryHive registryHive = RegistryHive.LocalMachine)
{
var registryKey = GetRegistryKey(keyPath,registryHive);
if (registryKey == null) return null;
var value = registryKey.GetValue(null);
if (value == null) return null;
return value;
}
public static bool HasSubKey(string keyPath,RegistryHive registryHive = RegistryHive.LocalMachine)
{
var rootKey = GetRegistryKey(keyPath,registryHive);
if (rootKey == null) return false;
var ret = rootKey.GetSubKeyNames().Contains(key);
rootKey.Close();
return ret;
}
public static void DeleteKey(string keyPath,registryHive);
if (rootKey == null) return;
rootKey.DeleteSubKey(key);
rootKey.Close();
}
public static void CreateRegistryKey(string parentPath,string subKey,string value,RegistryValueKind kind = RegistryValueKind.String)
{
RegistryKey rootKey = Registry.CurrentUser.OpenSubKey(parentPath,true);
if (rootKey == null) return;
RegistryKey newKey = rootKey.CreateSubKey(subKey);
newKey.SetValue(key,kind);
rootKey.Close();
}
/// <summary>
/// 16进制原码字符串转字节数组,用于设置二进制的值
/// </summary>
/// <param name="hexString">"AABBCC"或"AA BB CC"格式的字符串</param>
/// <returns></returns>
public static byte[] ConvertHexStringToBytes(string hexString)
{
hexString = hexString.Replace(" ","");
if (hexString.Length % 2 != 0)
{
throw new ArgumentException("参数长度不正确,必须是偶数位。");
}
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
{
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2,2),16);
}
return returnBytes;
}
}
设置不同类型的键值示例:
// 获取目标键
var subKey= RegistryHelper.GetRegistryKey(@"SYSTEM\CurrentControlSet\Services\xxx");
subKey.SetValue("Type",16,Microsoft.Win32.RegistryValueKind.DWord); // 数值类型
subKey.SetValue("DisplayName","Security Driver Service",Microsoft.Win32.RegistryValueKind.String); // 字符串类型
subKey.SetValue("ImagePath",@"C:\Program Files\xxx\aaa\bbb.exe -s ccc",Microsoft.Win32.RegistryValueKind.ExpandString); // 字符串拓展
// 十六进制转二进制类型
var securityKey = subKey.CreateSubKey("Security",true);
var securityStr = "01,00,14,80,90,9c,30,02,1c,01,ff,0f,60,04,fd,05,12,18,20,8d,0b,23,00";
var securityByt = RegistryHelper.ConvertHexStringToBytes(securityStr.Replace(",",""));
securityKey.SetValue("Security",securityByt,Microsoft.Win32.RegistryValueKind.Binary);
补充说明
操作注册表的应用程序权限一般需要提权至管理员才行,如果不方便也可以考虑采用拼接命令行的方式,通过调用cmd.exe来实现。
原文地址:https://blog.csdn.net/rjcql/article/details/134217561
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。