建立一个对注册表的常用操作的工具类。在HKEY_LOCAL_MACHINE/SOFTWARE下,增加项,判断指定项是否存在,增加键(键的名称,键的数值),判断指定的键是否存在
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- //倒入必须的命名空间
- using Microsoft.Win32;
- using Maticsoft.DBUtility;
- namespace Utility
- {
- /// <summary>
- /// 注册表操作,在HKEY_LOCAL_MACHINE/SOFTWARE下,增加项,判断指定项是否存在,增加键(键的名称,键的数值),判断指定的键是否存在
- /// </summary>
- public class RegeditUtil
- {
- private static string _itemname = string.Empty;
- /// <summary>
- /// 注册表左侧的项(左半边带有文件夹图标的节点)
- /// </summary>
- public static string ItemName{
- set
- {
- _itemname = value;
- }
- get
- {
- return _itemname;
- }
- }
- /// <summary>
- /// 增加键,如果没有,创建
- /// </summary>
- /// <param name="keyName">键的名称</param>
- /// <param name="keyValue">键的数值</param>
- public static void RegeditAdd(string keyName, string keyValue)
- {
- //首先判断是否存在ItemName项,没有就建立
- if (!RegeditItemExist(ItemName))//
- {
- //获取HKEY_LOCAL_MACHINE节点
- RegistryKey key = Registry.LocalMachine;
- //建立该节点,如果该节点已经存在,则不影响
- key.CreateSubKey("SOFTWARE//" + ItemName);
- }
- if (!RegeditKeyExist(keyName))//然后判断Zane项中是否有zane键,没有就添加默认密码记录
- {
- RegistryKey key = Registry.LocalMachine;
- RegistryKey software = key.OpenSubKey("SOFTWARE//"+ItemName, true); //该项必须已存在
- software.SetValue(keyName, keyValue);
- }
- }
- /// <summary>
- /// 根据节点的名称,验证该节点是否存在
- /// </summary>
- /// <returns></returns>
- public static bool RegeditItemExist(string itemName)
- {
- string[] subkeyNames;
- RegistryKey hkml = Registry.LocalMachine;
- RegistryKey software = hkml.OpenSubKey("SOFTWARE");
- //如果注册表中不存在该项则返回
- if (software == null)
- return false;
- subkeyNames = software.GetSubKeyNames();
- //取得该项下所有子项的名称的序列,并传递给预定的数组中
- foreach (string keyName in subkeyNames) //遍历整个数组
- {
- //判断子项的名称
- if (keyName == itemName)
- {
- hkml.Close();
- return true;
- }
- }
- hkml.Close();
- return false;
- }
- /// <summary>
- /// 根据键的名称,验证该键是否存在
- /// </summary>
- /// <param name="keyName">键的名称</param>
- /// <returns></returns>
- public static bool RegeditKeyExist(string keyName)
- {
- string[] subkeyNames;
- RegistryKey hkml = Registry.LocalMachine;
- RegistryKey software = hkml.OpenSubKey("SOFTWARE//"+ItemName);
- //如果注册表中不存在该项则返回
- if (software == null)
- return false;
- subkeyNames = software.GetValueNames();
- //取得该项下所有键值的名称的序列,并传递给预定的数组中
- foreach (string itemKeyName in subkeyNames)
- {
- if (itemKeyName == keyName) //判断键值的名称
- {
- hkml.Close();
- return true;
- }
- }
- hkml.Close();
- return false;
- }
- /// <summary>
- /// 根据键的名称,获得该键的数据
- /// </summary>
- /// <param name="keyName">键的名称</param>
- /// <returns></returns>
- public static string GetValeByName(string keyName)
- {
- RegistryKey Key = Registry.LocalMachine;
- RegistryKey Zane = Key.OpenSubKey("SOFTWARE//" + ItemName, true);
- string regName = Zane.GetValue(keyName).ToString();
- Zane.Close();
- return regName;
- }
- /// <summary>
- /// 根据键的名称,删除该键(值)
- /// </summary>
- /// <param name="keyName">键的名称</param>
- /// <returns></returns>
- public static bool DeleteRegist(string keyName)
- {
- string[] keys;
- RegistryKey hkml = Registry.LocalMachine;
- RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
- RegistryKey item = software.OpenSubKey(ItemName, true);
- //如果注册表中不存在该项则返回
- if (item == null)
- return false;
- keys = item.GetValueNames();
- foreach (string key in keys)
- {
- if (key == keyName)
- {
- item.DeleteValue(key);
- return true;
- }
- }
- return true;
- }
- }
- }
使用:
- private void button1_Click(object sender, EventArgs e)
- {
- //指定项的名称为BaiDu
- Utility.RegeditUtil.ItemName = "BaiDu";
- //再在该项中增加键(键名称,键的数值)
- Utility.RegeditUtil.RegeditAdd("aaaa", "1111111111");
- }
运行效果: