以下从‘读’‘写’‘删除’‘判断’四个事例实现对注册表的简单操作

以下从‘读’‘写’‘删除’‘判断’四个事例实现对注册表的简单操作
1.读取指定名称的注册表的值
private string GetRegistData(string name)
{
   string registData;
   RegistryKey hkml = Registry.LocalMachine;
   RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
   RegistryKey aimdir = software.OpenSubKey("XXX",true);
   registData = aimdir.GetValue(name).ToString();
   return registData;
}
以上是读取的注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下的XXX目录中名称为name的注册表值;

2.向注册表中写数据
private void WTRegedit(string name,string tovalue)
{
   RegistryKey hklm = Registry.LocalMachine;
   RegistryKey software = hklm.OpenSubKey("SOFTWARE",true);
   RegistryKey aimdir = software.CreateSubKey("XXX");
   aimdir.SetValue(name,tovalue);
}
以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下新建XXX目录并在此目录下创建名称为name值为tovalue的注册表项;

3.删除注册表中指定的注册表项
private void DeleteRegist(string name)
{
   string[] aimnames;
   RegistryKey hkml = Registry.LocalMachine;
   RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
   RegistryKey aimdir = software.OpenSubKey("XXX",true);
   aimnames = aimdir.GetSubKeyNames();
   foreach(string aimKey in aimnames)
   {
    if(aimKey == name)
     aimdir.DeleteSubKeyTree(name);
   }
}
以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下XXX目录中删除名称为name注册表项;

4.判断指定注册表项是否存在
private bool IsRegeditExit(string name)
{
   bool _exit = false;
   string[] subkeyNames;
   RegistryKey hkml = Registry.LocalMachine;
   RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
   RegistryKey aimdir = software.OpenSubKey("XXX",true);
   subkeyNames = aimdir.GetSubKeyNames();
   foreach(string keyName in subkeyNames)
   {
    if(keyName == name)
    {
       _exit = true;
       return _exit;
    }
   }
   return _exit;
}
以上是在注册表中HKEY_LOCAL_MACHINE/SOFTWARE目录下XXX目录中判断名称为name注册表项是否存在,这一方法在删除注册表时已经存在,在新建一注册表项时也应有相应判断;

 

 

下面给的例子首先是比较普遍的,层级少的,所以每级都会定义一个      RegistryKey

//读取指定键路径的值
private string GetRegistData(string name)
{
      string registData;
      RegistryKey hkml = Registry.LocalMachine;
      RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
      RegistryKey aimdir = software.OpenSubKey("XXX",true);
      registData = aimdir.GetValue(name).ToString();
      return registData;
}
//创建新值
private void WTRegedit(string name,string tovalue)
{
      RegistryKey hklm = Registry.LocalMachine;
      RegistryKey software = hklm.OpenSubKey("SOFTWARE",true);
      RegistryKey aimdir = software.CreateSubKey("XXX");
      aimdir.SetValue(name,tovalue);
}
//删除指定值
private void DeleteRegist(string name)
{
      string[] aimnames;
      RegistryKey hkml = Registry.LocalMachine;
      RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
      RegistryKey aimdir = software.OpenSubKey("XXX",true);
      aimnames = aimdir.GetSubKeyNames();
      foreach(string aimKey in aimnames)
      {
       if(aimKey == name)
        aimdir.DeleteSubKeyTree(name);
      }
}
//判断指定键是否存在
private bool IsRegeditExit(string name)
{
      bool _exit = false;
      string[] subkeyNames;
      RegistryKey hkml = Registry.LocalMachine;
      RegistryKey software = hkml.OpenSubKey("SOFTWARE",true);
      RegistryKey aimdir = software.OpenSubKey("XXX",true);
      subkeyNames = aimdir.GetSubKeyNames();
      foreach(string keyName in subkeyNames)
      {
       if(keyName == name)
       {
          _exit = true;
          return _exit;
       }
      }
      return _exit;
}

再给个实际的例子,如何通过C#获得"本地连接"列表名

public     void CreateList()
           {

               RegistryKey RegKey = Registry.LocalMachine;

               RegKey = RegKey.OpenSubKey(@"SYSTEM/ControlSet001/Control/Network/{4D36E972-E325-11CE-BFC1-08002BE10318}");

               string[] KeysList = null;
               if(RegKey.GetSubKeyNames().Length>0)
                KeysList = RegKey.GetSubKeyNames();       //Get List

            foreach (string Key in KeysList)
            {

                //Except "useable" key--"Connection",it's other names "Descriptions";

                if (Key == "Descriptions")

                    continue;

                RegistryKey SubKey = RegKey.OpenSubKey(Key);

                SubKey = SubKey.OpenSubKey("Connection");

                comboBox1.Items.Add(SubKey.GetValue("Name"));

            }

           }

最后,再给一个完整的读注册表的类的

/// <summary>
          /// 从注册表读取信息
         /// </summary>
         /// <param >路径</param>
         /// <param >值名</param>
         /// <returns>数据</returns>
          private object GetRegValue(string strRegPath, string strName)
          {
              strRegPath = strRegPath.Trim();
              //接收值的对象  
              object objRet;

              // 如果名称为空,则抛出一个参数为空的异常。   
              if (strName == "")
              {
                  throw new ArgumentNullException(strName, "键值不能为空!");
              }

              //去除"/"字符
              if (strRegPath.StartsWith("//"))
              {
                  strRegPath = strRegPath.Substring(1, strRegPath.Length - 1);
              }
              if (strRegPath.EndsWith("//"))
              {
                  strRegPath = strRegPath.Substring(0, strRegPath.Length - 1);
              }


              //拆分根键和路径   
              string strRootKey, strPath;
              int intIndex = strRegPath.IndexOf("//");

              strRootKey = strRegPath.Substring(0, intIndex).ToUpper();

              strPath = strRegPath.Substring(intIndex + 1, strRegPath.Length - intIndex - 1);
              RegistryKey _root;
              switch (strRootKey)
              {
                  case "HKEY_CLASSES_ROOT":
                      _root = Registry.ClassesRoot;
                      break;
                  case "HKEY_CURRENT_CONFIG":
                      _root = Registry.CurrentConfig;
                      break;
                  case "HKEY_CURRENT_USER":
                      _root = Registry.CurrentUser;
                      break;
                  case "HKEY_DYN_DATA":
                      _root = Registry.DynData;
                      break;
                  case "HKEY_LOCAL_MACHINE":
                      _root = Registry.LocalMachine;
                      break;
                  case "HKEY_PERFORMANCE_DATA":
                      _root = Registry.PerformanceData;
                      break;
                  case "HKEY_USERS":
                      _root = Registry.Users;
                      break;
                  default:
                      throw new Exception("找不到路径!");
              }

              try
              {
                  //打开注册表路径的键    
                  RegistryKey regKey = _root.OpenSubKey(@strPath);
                  //取值
                  objRet = regKey.GetValue(strName);
              }
              catch (Exception e)
              {
                  throw e;
              }

              return objRet;
          }

实例:string Url = GetRegValue(@"HKEY_LOCAL_MACHINE/SOFTWARE/Mysoft/paeq/ServicePath","ServicePath").ToString

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值