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 }
  • 相关阅读:
    用递归实现因式分解
    linux文件 面试知识
    嵌入式软件面试小点
    带环链表的几个问题
    C++对象的内存布局以及虚函数表和虚基表
    手把手教你用 React Hooks 开发移动端网站,从入门到实践
    你闺女都能看懂的 Kubernetes 插画指南!
    使用卷积神经网络识别交通标志
    6 个前端开发必备工具,提高你的生产力
    给新手看的 Micronaut 入门教程,10 分钟写出一个Micronaut程序
  • 原文地址:https://www.cnblogs.com/quietwalk/p/3547467.html
Copyright © 2011-2022 走看看