![]() |
C#操作注册表(读写)-用得还行
http://hi.baidu.com/iaskall/blog/it...4c935807f7.html
下面给的例子首先是比较普遍的,层级少的,所以每级都会定义一个 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() |
所有的时间均为北京时间。 现在的时间是 02:12 AM. |