zoukankan      html  css  js  c++  java
  • C# 操作注册表

      
            //Get Registry item value of key, item name=name
            public string GetValue(RegistryKey rootKey, string path, string itemName)
            {
                if (string.IsNullOrEmpty(itemName) || rootKey == null || string.IsNullOrEmpty(path)) return null;
    
                try
                {
                    RegistryKey regKey = rootKey.OpenSubKey(path, false);
                    if (regKey != null)
                    {
                        string itemValue = regKey.GetValue(itemName) == null ? null : regKey.GetValue(itemName).ToString();
    
                        if (!string.IsNullOrEmpty(itemValue))
                        {
                            regKey.Close();
                            return itemValue;
                        }
                    }
                }
                catch (Exception ex)
                {
    rootKey.Close();
    throw ex; } return null; #region Sample Code for Get item Value //string serverTypeRegPath = @"SYSTEMCurrentControlSetServicesW32TimeParameters"; //string itemName = "Type"; //RegistryKey rootKey = Registry.LocalMachine; //string strServerType = this.GetValue(rootKey, serverTypeRegPath, itemName); #endregion }
      //Set registry item value of key, item name=item name, item value=itemValue, item value type=itemValueTime
            //If the item dose not exixted, create the item and set the value
            public void SetValue(RegistryKey rootKey, string path, string itemName,
                RegistryValueKind itemValueKind, string itemValue)
            {
                if (rootKey == null || string.IsNullOrEmpty(path) || string.IsNullOrEmpty(itemName)
                    || itemValueKind == null || string.IsNullOrEmpty(itemValue))
                {
                    return;
                }
    
                try
                {
                    RegistryKey key = rootKey.OpenSubKey(path, true);
                    key.SetValue(itemName, itemValue, itemValueKind);
    
                    rootKey.Close();
                }
                catch (Exception ex)
                {
    rootKey.close();
    throw ex; } #region Sample Code //string serverTypeRegPath = @"SYSTEMCurrentControlSetServicesW32TimeParameters"; //string itemName = "test"; //string itemValue = "xlding_1"; //RegistryKey rootKey = Registry.LocalMachine; //this.SetValue(rootKey, serverTypeRegPath, itemName, RegistryValueKind.String, itemValue); //string strXXX = this.GetValue(rootKey, serverTypeRegPath, itemName); //MessageBox.Show(strXXX); #endregion }
  • 相关阅读:
    lua判断字符串包含另一个字符串
    expect使用技巧
    Linux expect
    expect正则捕获返回结果
    修改alpine Linux的Docker容器的时区
    Dockerfile镜像优化,减小镜像
    Sed在匹配行前后加入一行
    scp的使用以及cp的对比
    rsync 的用法
    傅里叶系列(一)傅里叶级数的推导 (转)
  • 原文地址:https://www.cnblogs.com/quietwalk/p/3547467.html
Copyright © 2011-2022 走看看