zoukankan      html  css  js  c++  java
  • c# 讀取、更新注冊表

        Asp.net更新客戶端注冊表,會涉及到安全性的問題,所以無法讀取及更新客戶端。這里代碼只能讀取及更新本機電腦注冊表。

    using Microsoft.Win32;


        /// <summary>
        
    /// 得到電腦名
        
    /// </summary>
        string sComputerName = System.Environment.MachineName;
        //注冊表中的路徑「SOFTWARE\CmWeb」
        const string sRegistryPath = @"SOFTWARE\CmWeb";

    1、讀取注冊表
        /// <summary>
        
    /// 得到注冊表內容
        
    /// </summary>
        void GetRegistry()
        {
            
    //打開指定路麼的注冊表
            rkKey = Registry.LocalMachine.OpenSubKey(sRegistryPath);
            
    if (rkKey != null)
            {
                
    //得到注冊表內容
                string sRegistryValue = rkKey.GetValue(sRegistryKey, "").ToString();
            }
        }

    2、創建注冊表
        /// <summary>
        
    /// 創建注冊表內容
        
    /// </summary>
        private void CreateRegistry()
        {
            
    //得到「HKEY_LOCAL_MACHINE\SOFTWARE」主鍵
            RegistryKey rkRootKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE",true);
            
    if (rkRootKey != null)
            {
                
    //不存在CmWeb時創建CmWeb
                if (Array.IndexOf(rkRootKey.GetSubKeyNames(), "CmWeb"< 0)
                {
                    rkRootKey.CreateSubKey(
    "CmWeb");
                }
                
    //得到「HKEY_LOCAL_MACHINE\SOFTWARE\CMWEB」鍵
                rkKey = Registry.LocalMachine.OpenSubKey(sRegistryPath,true);
                
    if (rkKey != null)
                {
                    
    //電腦名
                    string sRegValue = sComputerName;

                    
    //創建鍵值
                    rkKey.SetValue(sRegistryKey, sRegValue);
                }
            }
        }

    3、更新注冊表

        
    /// <summary>
        
    /// 更新注冊表
        
    /// </summary>
        void UpdateRegistry()
        {
            
    //得到「HKEY_LOCAL_MACHINE\SOFTWARE」主鍵
            RegistryKey rkRootKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE",true);
            
    if (rkRootKey != null)
            {
                
    //不存在「CmWeb」鍵時創建CmWeb
                if (Array.IndexOf(rkRootKey.GetSubKeyNames(), "CmWeb"< 0)
                {
                    
    //創建鍵名
                    rkRootKey.CreateSubKey("CmWeb");
                }
                
    //得到「HKEY_LOCAL_MACHINE\SOFTWARE\CMWEB」鍵
                rkKey = Registry.LocalMachine.OpenSubKey(sRegistryPath,true);
                
    if (rkKey != null)
                {
                    
    //電腦名
                    string sRegValue = sComputerName;

                    
    //創建鍵值
                    rkKey.SetValue(sRegistryKey, sRegValue);
                }
            }
        }


  • 相关阅读:
    Wannafly Winter Camp 2020 Day 7D 方阵的行列式
    [CF1311F] Moving Points
    [CF1311E] Construct the Binary Tree
    [CF1311D] Three Integers
    [CF1311C] Perform the Combo
    [CF1311B] WeirdSort
    [CF1311A] Add Odd or Subtract Even
    Wannafly Winter Camp 2020 Day 7A 序列
    SP7258 SUBLEX
    Wannafly Winter Camp 2020 Day 6J K重排列
  • 原文地址:https://www.cnblogs.com/scottckt/p/1166134.html
Copyright © 2011-2022 走看看