以下从‘读’‘写’‘删除’‘判断’四个事例实现对注册表的简单操作
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