.NET使用P/Invoke来实现注册表的增、删、改、查功能
1..NET使用P/Invoke来实现注册表的增、删、改、查功能
2..NET实现获取NTP服务器时间并同步(附带Windows系统启用NTP服务功能)3.工业福利!用.NET快速开发物联网扫码器设备的通用扫码功能4.上位机开发福利!快速掌握.NET中的Modbus通信5.使用Wesky.Net.Opentools库,一行代码实现实体类类型转换为Json格式字符串6.使用Wesky.Net.Opentools库,一行代码实现自动解析实体类summary注释信息(可用于数据实体文档的快速实现)7.谁说.net core不好动态访问webservice?看这篇文章,C#快速实现动态访问webservice,兼容.net framework和.net core+8.使用Wesky.Net.OpenTools包来快速实现嵌套型结构体数据转换功能9.基于DPAPI+RDP技术实现本地打开远程程序,并映射到本地机器桌面上注册表可以用来进行存储一些程序的信息,例如用户的权限、或者某些值等,可以根据个人需要进行存储和删减。
当前注册表主目录:
引用包 Wesky.Net.OpenTools 1.0.5或者以上版本
操作演示:
创建注册表项
设置注册表值
读取注册表值
删除注册表值
删除注册表项
操作演示代码
IRegistryManager registryManager = new RegistryManager();
// 创建注册表项
// registryManager.CreateKey(RegistryRoot.CurrentUser, @"Wesky\MyApp");
// 设置注册表值
// registryManager.SetValue(RegistryRoot.CurrentUser, @"Wesky\MyApp", "MyValue", "Hello, Registry!");
// 读取注册表值
// var value = registryManager.GetValue(RegistryRoot.CurrentUser, @"Wesky\MyApp", "MyValue");
// Console.WriteLine($"读取到的注册表值:{value}");
// 删除注册表值
// registryManager.DeleteValue(RegistryRoot.CurrentUser, @"Wesky\MyApp", "MyValue");
// 删除注册表项
registryManager.DeleteKey(RegistryRoot.CurrentUser, @"Wesky\MyApp");
Console.WriteLine("Over");
Console.ReadKey();
核心包内源码:
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegCreateKeyEx(
IntPtr hKey,
string lpSubKey,
int Reserved,
string lpClass,
int dwOptions,
int samDesired,
IntPtr lpSecurityAttributes,
out IntPtr phkResult,
out int lpdwDisposition);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegOpenKeyEx(
IntPtr hKey,
string lpSubKey,
int ulOptions,
int samDesired,
out IntPtr phkResult);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegCloseKey(IntPtr hKey);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegSetValueEx(
IntPtr hKey,
string lpValueName,
int Reserved,
int dwType,
byte[] lpData,
int cbData);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegGetValue(
IntPtr hKey,
string lpSubKey,
string lpValue,
int dwFlags,
out int pdwType,
StringBuilder pvData,
ref int pcbData);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegDeleteKey(IntPtr hKey, string lpSubKey);
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int RegDeleteValue(IntPtr hKey, string lpValueName);
/// <summary>
/// 获取注册表根键
/// Get registry root key
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
private IntPtr GetRegistryRootKey(RegistryRoot root)
{
switch (root)
{
case RegistryRoot.ClassesRoot:
return HKEY_CLASSES_ROOT;
case RegistryRoot.CurrentUser:
return HKEY_CURRENT_USER;
case RegistryRoot.LocalMachine:
return HKEY_LOCAL_MACHINE;
case RegistryRoot.Users:
return HKEY_USERS;
case RegistryRoot.CurrentConfig:
return HKEY_CURRENT_CONFIG;
default:
throw new ArgumentOutOfRangeException(nameof(root), root, null);
}
}
/// <summary>
/// 创建注册表键
/// Create registry key
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <exception cref="Exception"></exception>
public void CreateKey(RegistryRoot root, string subKey)
{
IntPtr hKey = GetRegistryRootKey(root);
int result = RegCreateKeyEx(hKey, subKey, 0, null, REG_OPTION_NON_VOLATILE, KEY_WRITE, IntPtr.Zero, out IntPtr phkResult, out _);
if (result != ERROR_SUCCESS)
{
throw new Exception("创建注册表key失败。 Failed to create registry key.");
}
RegCloseKey(phkResult);
}
/// <summary>
/// 删除注册表键
/// Delete registry key
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <exception cref="Exception"></exception>
public void DeleteKey(RegistryRoot root, string subKey)
{
IntPtr hKey = GetRegistryRootKey(root);
int result = RegDeleteKey(hKey, subKey);
if (result != ERROR_SUCCESS)
{
throw new Exception("删除注册表key失败。Failed to delete registry key.");
}
}
/// <summary>
/// 设置注册表值
/// Set registry value
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="valueName"></param>
/// <param name="value"></param>
/// <exception cref="Exception"></exception>
public void SetValue(RegistryRoot root, string subKey, string valueName, string value)
{
IntPtr hKey = GetRegistryRootKey(root);
int result = RegOpenKeyEx(hKey, subKey, 0, KEY_WRITE, out IntPtr phkResult);
if (result != ERROR_SUCCESS)
{
throw new Exception("打开注册表key失败。Failed to open registry key.");
}
byte[] data = Encoding.Unicode.GetBytes(value);
result = RegSetValueEx(phkResult, valueName, 0, REG_SZ, data, data.Length);
if (result != ERROR_SUCCESS)
{
throw new Exception("设置注册表值失败。Failed to set registry value.");
}
RegCloseKey(phkResult);
}
/// <summary>
/// 获取注册表值
/// Get registry value
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="valueName"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public string GetValue(RegistryRoot root, string subKey, string valueName)
{
IntPtr hKey = GetRegistryRootKey(root);
int result = RegOpenKeyEx(hKey, subKey, 0, KEY_READ, out IntPtr phkResult);
if (result != ERROR_SUCCESS)
{
throw new Exception("打开注册表key失败。Failed to open registry key.");
}
int type = 0;
int size = 1024;
StringBuilder data = new StringBuilder(size);
result = RegGetValue(phkResult, null, valueName, RRF_RT_REG_SZ, out type, data, ref size);
if (result != ERROR_SUCCESS)
{
throw new Exception("获取注册表的值失败。Failed to get registry value.");
}
RegCloseKey(phkResult);
return data.ToString();
}
/// <summary>
/// 删除注册表值
/// Delete registry value
/// </summary>
/// <param name="root"></param>
/// <param name="subKey"></param>
/// <param name="valueName"></param>
/// <exception cref="Exception"></exception>
public void DeleteValue(RegistryRoot root, string subKey, string valueName)
{
IntPtr hKey = GetRegistryRootKey(root);
int result = RegOpenKeyEx(hKey, subKey, 0, KEY_WRITE, out IntPtr phkResult);
if (result != ERROR_SUCCESS)
{
throw new Exception("打开注册表key失败。Failed to open registry key.");
}
result = RegDeleteValue(phkResult, valueName);
if (result != ERROR_SUCCESS)
{
throw new Exception("删除注册表的值失败。Failed to delete registry value.");
}
RegCloseKey(phkResult);
}
本文作者:Wesky
微信号:WeskyNet001
公众号:Dotnet Dancer
文章链接:https://www.cnblogs.com/weskynet/p/18191869

欢迎扫一扫关注公众号,发现更多其他技术分享
合集:
OpenTools
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器