记得当时刚接触C#的时候,喜欢编写各种小软件,而注册表系列和网络系列被当时的我认为大牛的必备技能。直到我研究注册表前一天我都感觉他是那么的高深。

今天正好有空,于是就研究了下注册表系列的操作,也随手封装了一个注册表帮助类。简单记一下,当饭后娱乐

完整Demo研究:https://github.com/dunitian/LoTCodeBase/tree/master/NetCode/0.知识拓展/02.注册表系

这个是一些常用的方法和属性(不全,只是列出了比较常用的一些)【OpenSubKey(string name,bool b)当b为true则表示开了可写权限】

//RegistryKey
//属性:
// ValueCount 检索项中值的计数
// SubKeyCount 获取子项个数

//方法:
// OpenSubKey(string name,bool b) 获取子项 RegistryKey,b为true时代表可写
// GetSubKeyNames() 获取所有子项名称的字符串数组
// GetValueNames() 检索包含与此项关联的所有值名称的字符串数组
// GetValue(string name) 获取指定名称,不存在名称/值对,则返回 null
// CreateSubKey(string subkey) 创建或者打开子项的名称或路径
// SetValue(string name,object value) 创建或者打开子项的名称或路径
// DeleteSubKeyTree(string subkey) 递归删除指定目录,不存在则抛异常
// DeleteSubKey(string subkey,bool b) 删除子项,b为false则当子项不存在时不抛异常
// DeleteValue(string name,bool b) 删除指定的键值,b为false则当子项不存在时不抛异常

先举个简单的案例:

代码如下:

//获取一个表示HKLM键的RegistryKey
RegistryKey rk = Registry.LocalMachine; //打开HKLM的子项Software
RegistryKey subKey = rk.OpenSubKey(@"software"); //遍历所有子项名称的字符串数组
foreach (var item in subKey.GetSubKeyNames())
{
//以只读方式检索子项
RegistryKey sonKey = subKey.OpenSubKey(item); rtxt.AppendText(string.Format("\n--->{0}<---\nSubKeyCount:{1} ValueCount:{2} FullName:{3}\n==================================\n", item, sonKey.SubKeyCount, sonKey.ValueCount, sonKey.Name)); //检索包含与此项关联的所有值名称的字符串数组
foreach (var name in sonKey.GetValueNames())
{
rtxt.AppendText(string.Format("Name:{0} Value:{1} Type:{2}\n", name, sonKey.GetValue(name), sonKey.GetValueKind(name)));
}
}

做个综合的案例:

代码如下:

public partial class MainForm : Form
{
public RegistryKey Reg { get; set; }
public MainForm()
{
InitializeComponent();
//初始化
var rootReg = Registry.LocalMachine;
Reg = rootReg.OpenSubKey("software", true);//开权限
} #region 公用方法
/// <summary>
/// 检验是否为空
/// </summary>
/// <param name="dntReg"></param>
private bool KeyIsNull(RegistryKey dntReg)
{
if (dntReg == null)
{
rtxt.AppendText("注册表中没有dnt注册项\n");
return true;
}
return false;
} /// <summary>
/// 遍历Key的Value
/// </summary>
/// <param name="reg"></param>
private void ForeachRegKeys(RegistryKey reg)
{
rtxt.AppendText(string.Format("\n SubKeyCount:{0} ValueCount:{1} FullName:{2}\n", reg.SubKeyCount, reg.ValueCount, reg.Name));
foreach (var name in reg.GetValueNames())
{
rtxt.AppendText(string.Format("Name:{0} Value:{1} Type:{2}\n", name, reg.GetValue(name), reg.GetValueKind(name)));
}
}
#endregion //查
private void btn1_Click(object sender, EventArgs e)
{
var dntReg = Reg.OpenSubKey("dnt");
if (KeyIsNull(dntReg)) return;
ForeachRegKeys(dntReg);
foreach (var item in dntReg.GetSubKeyNames())
{
//以只读方式检索子项
RegistryKey sonKey = dntReg.OpenSubKey(item);
ForeachRegKeys(sonKey);
}
} //增
private void btn2_Click(object sender, EventArgs e)
{
var dntReg = Reg.CreateSubKey("dnt");
dntReg.SetValue("web", "http://www.dkill.net");
var sonReg = dntReg.CreateSubKey("path");
sonReg.SetValue("value", "D:\\Program Files\\dnt");
rtxt.AppendText("添加成功\n");
} //改
private void btn3_Click(object sender, EventArgs e)
{
var dntReg = Reg.OpenSubKey("dnt", true);
if (KeyIsNull(dntReg)) return;
dntReg.SetValue("web", "http://dnt.dkill.net");
rtxt.AppendText("修改成功\n");
} //删
private void btn4_Click(object sender, EventArgs e)
{
try
{
#region 删除某个值
//var dntReg = Reg.OpenSubKey("dnt", true);
//if (KeyIsNull(dntReg)) return;
//dntReg.DeleteValue("web", false);
#endregion
Reg.DeleteSubKeyTree("dnt", false);
rtxt.AppendText("删除成功\n");
}
catch (ArgumentException ex)
{
rtxt.AppendText(ex.ToString());
}
} private void clearlog_Click(object sender, EventArgs e)
{
rtxt.Clear();
}
}

Helper类综合实战:(有其他演示,有的电脑会出现权限问题)

using Microsoft.Win32;
using System.Collections.Generic; public static partial class RegistryHelper
{
#region 节点
/// <summary>
/// HKEY_LOCAL_MACHINE 节点
/// </summary>
public static RegistryKey RootReg = Registry.LocalMachine; /// <summary>
/// HKEY_LOCAL_MACHINE 下 Software 节点
/// </summary>
public static RegistryKey SoftReg = Registry.LocalMachine.OpenSubKey("software", true); /// <summary>
/// 包含有关当前用户首选项的信息。该字段读取 Windows 注册表基项 HKEY_CURRENT_USER
/// </summary>
public static RegistryKey CurrentUser = Registry.CurrentUser; /// <summary>
/// HKEY_CURRENT_USER 下 Software 节点
/// </summary>
public static RegistryKey UserSoftReg = Registry.CurrentUser.OpenSubKey("software", true);
#endregion #region 查询
/// <summary>
/// 根据名称查找Key,有则返回RegistryKey对象,没有则返回null
/// </summary>
/// <param name="name">要打开的子项的名称或路径</param>
/// <param name="b">如果不需要写入权限请选择false</param>
/// <returns></returns>
public static RegistryKey FindKey(this RegistryKey reg, string name, bool b = true)
{
return reg.OpenSubKey(name, b);
} /// <summary>
/// 获取(name,value)字典,没有则返回null
/// </summary>
/// <param name="reg">当前RegistryKey</param>
/// <returns></returns>
public static IDictionary<string, object> GetKeyValueDic(this RegistryKey reg)
{
var dic = new Dictionary<string, object>();
if (reg.ValueCount == 0) { return null; }
ForeachRegKeys(reg, ref dic);
return dic;
} /// <summary>
/// 获取子项(name,value)字典,没有则返回null
/// </summary>
/// <param name="reg">当前RegistryKey</param>
/// <returns></returns>
public static IDictionary<string, object> GetSubKeyValueDic(this RegistryKey reg)
{
var dic = new Dictionary<string, object>();
if (reg.SubKeyCount == 0) { return null; }
foreach (var item in reg.GetSubKeyNames())
{
//以只读方式检索子项
var sonKey = reg.OpenSubKey(item);
ForeachRegKeys(sonKey, ref dic);
}
return dic;
} /// <summary>
/// 遍历RegistryKey
/// </summary>
/// <param name="reg"></param>
/// <param name="dic"></param>
private static void ForeachRegKeys(RegistryKey reg, ref Dictionary<string, object> dic)
{
foreach (var name in reg.GetValueNames())
{
dic.Add(name, reg.GetValue(name));
}
}
#endregion #region 添加
/// <summary>
/// 添加一个子项
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static RegistryKey AddSubItem(this RegistryKey reg, string name)
{
return reg.CreateSubKey(name);
} /// <summary>
/// 添加key-value,异常则RegistryKey对象返回null
/// </summary>
/// <param name="reg"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static RegistryKey AddKeyValue(this RegistryKey reg, string key, object value)
{
reg.SetValue(key, value);
return reg;
}
#endregion #region 修改
/// <summary>
/// 修改key-value,异常则RegistryKey对象返回null
/// </summary>
/// <param name="reg"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static RegistryKey UpdateKeyValue(this RegistryKey reg, string key, object value)
{
return reg.AddKeyValue(key, value);
}
#endregion #region 删除
/// <summary>
/// 根据Key删除项
/// </summary>
/// <param name="reg"></param>
/// <param name="key"></param>
/// <returns></returns>
public static RegistryKey DeleteKeyValue(this RegistryKey reg, string key)
{
reg.DeleteValue(key, false);
return reg;
} /// <summary>
/// 删除子项所有内容
/// </summary>
/// <param name="reg"></param>
/// <param name="key"></param>
/// <returns></returns>
public static RegistryKey ClearSubAll(this RegistryKey reg, string key)
{
reg.DeleteSubKeyTree(key, false);
return reg;
}
#endregion
}

弥补学生时代的遗憾~C#注册表情缘的更多相关文章

  1. 10#Windows注册表的那些事儿

    引言 用了多年的Windows系统,其实并没有对Windows系统进行过深入的了解,也正是由于Windows系统不用深入了解就可以简单上手所以才有这么多人去使用.笔者是做软件开发的,使用的基本都是Wi ...

  2. C#注册表

    C#注册表情缘   记得当时刚接触C#的时候,喜欢编写各种小软件,而注册表系列和网络系列被当时的我认为大牛的必备技能.直到我研究注册表前一天我都感觉他是那么的高深. 今天正好有空,于是就研究了下注册表 ...

  3. [转]Windows系统注册表知识完全揭密

    来源:http://www.jb51.net/article/3328.htm Windows注册表是帮助Windows控制硬件.软件.用户环境和Windows界面的一套数据文件,注册表包含在Wind ...

  4. 注册表与盘符(转victor888文章 )

    转自: http://blog.csdn.net/loulou_ff/article/details/3769479     写点东西,把这阶段的研究内容记录下来,同时也给研究相关内容的同志提供参考, ...

  5. 64位WINDOWS系统环境下应用软件开发的兼容性问题(CPU 注册表 目录)

    应用软件开发的64 位WINDOWS 系统环境兼容性 1. 64 位CPU 硬件 目前的64位CPU分为两类:x64和IA64.x64的全称是x86-64,从名字上也可以看出来它和 x86是兼容的,原 ...

  6. 【转】如何打开注册表编辑器中存储用户信息的SAM文件?

    sam文件怎么打开 (Security Accounts Manager安全帐户管理器)负责SAM数据库的控制和维护.SAM数据库位于注册表HKLM\SAM\SAM下,受到ACL保护,可以使用rege ...

  7. python测试开发django-35.xadmin注册表信息

    前言 xadmin后台如果要对表的内容增删改查,跟之前的admin.py文件里面写注册表信息一样,需在admin.py同一级目录新建一个adminx.py的文件. 然后在adminx.py文件控制页面 ...

  8. Dll注入技术之注册表注入

    DLL注入技术之REG注入 DLL注入技术指的是将一个DLL文件强行加载到EXE文件中,并成为EXE文件中的一部分,这样做的目的在于方便我们通过这个DLL读写EXE文件内存数据,(例如 HOOK EX ...

  9. cmd中删除、添加、修改注册表命令

    转自:http://www.jb51.net/article/30586.htm regedit的运行参数 REGEDIT [/L:system] [/R:user] filename1 REGEDI ...

随机推荐

  1. zabbix身份验证流程解析&绕过身份验证的方法

    由于实验室产品的监控模块的需求,需要绕过zabbix的验证模块,实现从二级平台到zabbix的无缝接入. 测试发现,zabbix的身份验证并不是想象的那么简单,为了实现功能,遂进行源码分析. zabb ...

  2. C#调用C++动态库方法及动态库封装总结

    我只是粗浅的学习过一些C++语法, 变量类型等基础内容, 如有不对的地方还望指出. 如果你跟我一样, 对指针操作不了解, 对封装C++动态库头疼的话, 下面内容还是有帮助的. 转载请注明出处: htt ...

  3. ZOJ-3820 Building Fire Stations 题解

    题目大意: 一棵树,在其中找两个点,使得其他点到这两个的距离的较小值的最大值的最小值及其方案. 思路: 首先显然一棵树的直径的中点到其他点的距离的最大值必定比其他点的小. 那么感性思考一下就将一棵树的 ...

  4. 获取设备UDID、IMEI、ICCID、序列号、Mac地址等信息

    在iOS7之前, 可以方便的使用 [[UIDevice currentDevice] uniqueIdentifier] 来获取设备的UDID,但是在iOS7之后这个方法不再适用. 你可以用[[UID ...

  5. StatePattern

    class Program { static void Main(string[] args) { var state = new OpeningState(); var lift = new Lif ...

  6. ASP.NET图形验证码的生成

    效果: 调用方法: int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列 );//产生验证码字符 pictureBox1.Image = QAPI.Ver ...

  7. Linux下Electron的Helloworld

    什么是Electron Electron 框架的前身是 Atom Shell,可以让你写使用 JavaScript,HTML 和 CSS 构建跨平台的桌面应用程序.它是基于io.js 和 Chromi ...

  8. 不可变数组NSArray

    //数组里面不允许存放基本数据类型,只能存放“对象” NSArray *array = [NSArray arrayWithObjects:@"周星星",@"尹天仇&qu ...

  9. 回忆那些我们曾今铭记过的.NET重点知识

    正如标题所说的那样,到底是那些.NET的知识点呢?     接下来就让我带着你们去了解这些知识点吧! 1.接口 2.索引器 3.FOREACH的本质 4.匿名内部类 5.运算符的重载 一.什么是接口? ...

  10. 公司培训 oracle( 第一天)

    以前在学校学习Oracle的时候就对rowid 和rownum 这两个伪列有很大的疑惑,今天公司对16届新员工进行公司内部技术培训,课堂上的讲解,又让我想起来了曾经的疑惑点, 我想不能在让这个疑惑继续 ...