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 }
  • 相关阅读:
    微信小程序开发之tab导航栏
    微信小程序开发之日期组件
    微信小程序运行机制
    iis部署错误:HTTP 错误 500.21
    web API请求与参数获取
    C#API接口调试工具
    微信小程序之自定义组件与使用
    微信小程序开发之拼接json数组字符串
    IdentityServer4 中文文档 -11- (快速入门)添加基于 OpenID Connect 的用户认证
    IdentityServer4 中文文档 -10- (快速入门)使用密码保护API
  • 原文地址:https://www.cnblogs.com/quietwalk/p/3547467.html
Copyright © 2011-2022 走看看