.NET使用P/Invoke来实现注册表的增、删、改、查功能

news/2024/9/22 5:29:15

注册表可以用来进行存储一些程序的信息,例如用户的权限、或者某些值等,可以根据个人需要进行存储和删减。

当前注册表主目录:

引用包 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);}

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/31500.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

IDEA社区版(IDEA Community Edition)创建Springboot父子项目

1. 因为社区办不支持使用spring Spring Initializr 的方式创建项目, 但是我们可以考虑使用别的方式达到效果: 创建方式有3种: 第一种:使用https://start.spring.io/ 官方URL创建项目,再导入到 IDEA Community Edition(后面简称:ideaC)。具体使用自行百度。缺点:没办法自定…

php查询结果汉字乱码解决方法

问题描述:使用php查询数据显示,显示的结果中所有汉字乱码问题及解决:这种情况是编码造成的,检查数据库及页面编码是否一致,也可在页面增加: header(Content-Type:text/html;charset=utf-8); 刷新后页面汉字显示正常,问题解决

设计模式学习(二)工厂模式——简单工厂模式

讨论设计模式中的简单工厂模式目录前言简单工厂模式简介示例优点缺点使用场景 前言 工厂模式是一种常用的设计模式,属于创建型模式之一。它的主要目的是为了解耦组件之间的依赖关系。通过使用工厂模式,系统中的具体类的实例化过程可以被抽象出来,从而使得系统更加模块化,增…

Pytest配置文件pytest.ini

pytest.ini 配置 pytest.ini 是什么pytest.ini 是 pytest 的配置文件 可以修改 pytest 的默认行为 不能使用任何中文符号,包括汉字、空格、引号、冒号等等pytest.ini修改用例的命名规则 配置日志格式,比代码配置更方便 添加标签,防止运行过程报警告错误 指定执行目录 排除搜…

使用ZXing.Net生成二维码

所需依赖组件 从工程安装的ZXing.Net Nuget包查看,ZXing.Net不依赖其他组件。查看package包内容,发现内部就zxing.dll和zxing.presentation.dll两个动态库文件。ZXing.Net生成的二维码形式 生成的二维码形式为内存Bitmap图像对象,如果需保存为文件或Base64字符串需另外书写代…

Netgear无线路由器漏洞复现(CVE-2019-20760)

本文复现的漏洞为Netgear路由器远程命令执行漏洞,1.0.4.26之前的NETGEAR R9000设备会受到身份验证绕过的影响,可利用漏洞将木马程序下载下来,获取 shell。漏洞概述 漏洞服务: uhttpd 漏洞类型: 远程命令执行 影响范围: 1.0.4.26之前的NETGEAR R9000设备会受到身份验证绕过…

经验分享:春招零Offer,5月份还有机会吗?

先说答案:5 月份依然有拿到 Offer 的机会。 5月份春招结束了吗?对于应届大学生来说(也就是今年暑假毕业的学生),5 月中旬春招就陆续结束了,但是 5 月份会有很多补录的机会。 对于非应届的大学生来说(今年之后毕业的学生)来说,5 月和 6 月正是在暑假最好的时机,尤其是…

PMS15

1. GPIO 相关的寄存器设置典型配置程序: //第一种配置方式 PA = 0b0110_0000; \ //设置数据 PAPH = 0b1110_0000; \ //设置上下拉 PAC = 0b1110_0000; \ //控制输入输出//第二种配置方式 PAC.4 = 0; …