• 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 }
  • 相关阅读:
    获取aspx页面执行时间完全解决方案
    WebForm中DataGrid的20篇经典文章
    不走寻常路 设计ASP.NET应用程序的七大绝招
    动态绑定dropdownlist 开始拣.NET
    Notes中几个处理多值域的通用函数
    Lotus开发之Lotus Notes中域的验证
    Undokumentierte @Formeln/LotusScript im Lotus Notes Client/Server
    domino server端的Notes.ini详解
    Lotus开发基本性能优化
    以Ajax方式显示Lotus Notes视图的javasript类库NotesView2
  • 原文地址:https://www.cnblogs.com/quietwalk/p/3547467.html
Copyright © 2020-2023  润新知