• 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");
    
        }
    
  • 相关阅读:
    将word转化为swf 进行如同百度文库的般阅读
    最大子数组问题——编程珠玑第八章
    为什么静态成员必须在类外初始化
    C++初始化列表
    异步消息总线hornetq学习-03客户端连接hornet进行jms消息的收发-非jndi方式连接
    [PLL][PM]锁相环模拟相位解调
    insertion sort
    SRM 581 D2 L2:SurveillanceSystem,重叠度
    JQuery(下)
    Ajax
  • 原文地址:https://www.cnblogs.com/scgw/p/1641919.html
Copyright © 2020-2023  润新知