C#操作注册表(读写)

下面给的例子首先是比较普遍的, 层级少的,所以每级都会定义一个      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 name="strRegPath">路径</param>
         /// <param name="strName">值名</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()

 

转载自:

http://hi.baidu.com/iaskall/blog/item/a8b03823ccf8df4c935807f7.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值