.NET使用P/Invoke来实现注册表的增、删、改、查功能
注册表可以用来进行存储一些程序的信息,例如用户的权限、或者某些值等,可以根据个人需要进行存储和删减。
当前注册表主目录:
引用包 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);
}
.NET使用P/Invoke来实现注册表的增、删、改、查功能的更多相关文章
- django单表操作 增 删 改 查
一.实现:增.删.改.查 1.获取所有数据显示在页面上 model.Classes.object.all(),拿到数据后,渲染给前端;前端通过for循环的方式,取出数据. 目的:通过classes(班 ...
- Django(十)模型:django模型类对数据库的:增/删/改/查、自关联、管理器、元选项(指定表名)
一.插入.更新和删除 调用一个模型类对象的save方法的时候就可以实现对模型类对应数据表的插入和更新. 调用一个模型类对象的delete方法的时候就可以实现对模型类对应数据表数据的删除. 二.自关联 ...
- oracle 11g 建库 建表 增 删 改 查 约束
一.建库 1.(点击左上角带绿色+号的按钮) 2.(进入这个界面,passowrd为密码.填写完后点击下面一排的Test按钮进行测试,无异常就点击Connect) 二.建表 1-1. create t ...
- 第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据
第18课-数据库开发及ado.net 连接数据库.增.删.改向表中插入数据并且返回自动编号.SQLDataReade读取数据 ADO.NET 为什么要学习? 我们要搭建一个平台(Web/Winform ...
- MySql——进阶一(库表的建 删 改)
基于学生管理系统建立 数据库 和 表 表的样式: 表(一)Student (学生use) 属性名 数据类型 可否为空 含 义 Sno varchar (20) 否 学号(主码) Sname varch ...
- 注册表 锁IE首页
用附件中的修改软件,或者用以下修改注册表的办法. 一.注册表被修改的原因及解决办法 其实,该恶意网页是含有有害代码的ActiveX网页文件,这些广告信息的出现是因为浏览者的注册表被恶意更改的结果. ...
- 学习window系统下的注册表
一直不明白注册表是一个什么鬼,查了资料后大概明白了注册表到底有什么用,其实简单来说注册表就是一个存放系统.硬件.应用配置信息的数据ku.##### 一.注册表的来历在最早的视窗操作系统win3.x中, ...
- S3c2440A WINCE平台HIVE注册表+binfs的实现
今天最大的收获莫过于把binfs和hive注册表同时在三星的平台上实现了,这可是前无古人啊(只是看到好多哥们说找不到三星的HIVE资料),哈哈哈.怕今天的成果日后成炮灰,还是写下来比较好,要养成这样的 ...
- 10#Windows注册表的那些事儿
引言 用了多年的Windows系统,其实并没有对Windows系统进行过深入的了解,也正是由于Windows系统不用深入了解就可以简单上手所以才有这么多人去使用.笔者是做软件开发的,使用的基本都是Wi ...
- maya2012卸载/安装失败/如何彻底卸载清除干净maya2012注册表和文件的方法
maya2012提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装maya2012失败提示maya2012安装未完成,某些产品无法安装,也有时候想重新安装maya ...
随机推荐
- #01背包,容斥,排列组合#洛谷 5615 [MtOI2019]时间跳跃
题目 分析 不是凸多边形当且仅当边数小于2或者最长边大于等于其余边之和, 那么容斥一下,首先总权值为 \[\sum_{i=1}^nC(n,i)\times i=n\sum_{i=1}^nC(n-1,i ...
- Jetty的bytebufferpool模块
bytebufferpool模块用于配置Jetty的ByteBuffer对象的对象池. 通过对象池的方式来管理ByteBuffer对象的使用和生命周期,期望降低Jetty进程内存的使用,同时降低JVM ...
- 8. Linear Transformations
8.1 Linear Requires Keys: A linear transformation T takes vectors v to vectors T(v). Linearity requi ...
- 使用Python-psycopg访问postgres、openGauss、MogDB
摘要 Psycopg 是一种用于执行 SQL 语句的 PythonAPI,可以为 PostgreSQL.GaussDB 数据库提供统一访问接口,应用程序可基于它进行数据操作.Psycopg2 是对 l ...
- HDC2021技术分论坛:还有人不知道鸿蒙智联设备认证咋搞?
作者:maxiansheng,华为鸿蒙智联认证测试专家 2021年5月18日,华为正式宣布原Work With HUAWEI HiLink和Powered by HarmonyOS品牌升级为Harmo ...
- CentOS8 / CentOS7 yum源最新修改搭建 2022.3.1
Part I CentOS 8 源更新 ========================================== 2022年过完后,发现公司里面的所有服务器yum都不能用了,一直报错 按照 ...
- nginx重新整理——————http请求的11个阶段[十二]
前言 已经到了关键的http请求的11个阶段了. 正文 概念图: 11 个阶段的处理顺序: 那么就来介绍一下: 先来了解一下postread阶段的realip这个处理,realip 是 real ip ...
- Reinforcement Learning (DQN) 中经验池详细解释
一般DQN中的经验池类,都类似于下面这段代码. import random from collections import namedtuple, deque Transition = namedtu ...
- Web前端 - Vue
<!-- id标识vue作用的范围 --> <div id="app"> <!-- {{}} 插值表达式,绑定vue中的data数据 --> { ...
- vue中img标签图片 加载时 与 加载失败 的处理方法
开发过程中经常需要和图片处理打交道,看了网上很多有关图片处理的方法,都是讲解得一知半解,没有比较全面的总结.下面,将简单总结一个我们通过vue去处理img标签过程中,图片加载时,与图片加载失败时的处理 ...