程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 注冊表Regedit讀寫

注冊表Regedit讀寫

編輯:C#入門知識

需要引入命名空間:
using Microsoft.Win32;
       /// 讀注冊表中指定鍵的值
        /// </summary>
        /// <param name="key">鍵名</param>
        /// <returns>返回鍵值</returns>
        public static string ReadReg(string key)
        {
            string temp = "";
            try
            {
                RegistryKey key = Registry.LocalMachine;                      //Registry參數自己選擇
                RegistryKey subKey = Key.OpenSubKey("SOFTWARE\Exobo");      //注冊表位置
                temp = subKey.GetValue(key).ToString();
                subKey.Close();
                key .Close();
                return temp;
            }
            catch (Exception)
            {
                throw;
            }
        }
       /// <summary>
        /// 創建目錄(第一次注冊)---寫入
       /// </summary>
       /// <param name="username"></param>
       /// <param name="password"></param>
       /// <param name="userType">版本類型</param>
       /// <param name="userMax">最大試用次數</param>
       /// <param name="userTime">當前試用次數</param>
       /// <returns></returns>
        public static bool CreateReg(string username,string password,string userType,string userMax,string userTime)
        {
            bool temp = false;
            try
            {
                RegistryKey Key = Registry.LocalMachine;
                RegistryKey subKey = Key.OpenSubKey(@"SOFTWAREExobo");
                //未注冊 從新創建
                if (subKey == null)
                {
                    Key.CreateSubKey("SOFTWARE\Exobo");
                }
              //修改注冊信息
                subKey = Key.OpenSubKey("SOFTWARE\Exobo", true);
                subKey.SetValue(username, password);
                subKey.SetValue(username + "Type", userType);
                subKey.SetValue(username + "Time", userTime);
                subKey.SetValue(username + "Max", userMax);             
                temp = true;
                subKey.Close();
                Key.Close();
                return temp;

            }
            catch (Exception)
            {
                throw;
            }

        }
   //判段是否有該項
        public static bool HaveReg()
        {
            bool temp = false;
            try
            {
                RegistryKey Key = Registry.LocalMachine;
                RegistryKey subKey = Key.OpenSubKey(@"SOFTWAREExobo");
                //未注冊 從新創建
                if (subKey == null)
                {
                    temp = false;
                }
                else
                {
                    temp = true;
        

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved