C#操作注册表

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;
            try
            {
                string[] subkeyNames;
                RegistryKey hkml = Registry.LocalMachine;
                RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
                RegistryKey aimdir = software.OpenSubKey("Weather", true);
                subkeyNames = aimdir.GetValueNames();
                foreach (string keyName in subkeyNames)
                {
                    if (keyName == name)
                    {
                        _exit = true;
                        return _exit;
                    }
                }
            }
            catch
            { }
            return _exit;
        }

以上是在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下XXX目录中判断名称为name注册表项是否存在,这一方法在删除注册表时已经存在,在新建一注册表项时也应有相应判断;  

一个例子:

using Microsoft.Win32;
    using System.Diagnostics;
    private void Access_Registry()
    {
         // 在HKEY_LOCAL_MACHINE\Software下建立一新键,起名为MCBInc
            RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
         // 增加一个子键
            RegistryKey newkey = key.CreateSubKey("MCBInc");
   
    // 设置此子键的值
            newkey.SetValue("MCBInc", "NET Developer");
         
  // 从注册表的其他地方获取数据
         
  // 找出你的CPU
            RegistryKey pRegKey = Registry.LocalMachine;
            pRegKey = pRegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
            Object val = pRegKey.GetValue("VendorIdentifier");
            Debug.WriteLine("The central processor of this machine is:"+ val);
         // 删除键值
            RegistryKey delKey = Registry.LocalMachine.OpenSubKey("Software", true);
            delKey.DeleteSubKey("MCBInc");
    }
posted @ 2008-04-09 10:32  白虎  阅读(8943)  评论(3编辑  收藏  举报
非常感谢dudu,感谢博客园提供的各种服务。
联系白虎(David.Du)