C# winform

博客园 首页 联系 订阅 管理
需要引入命名空间:
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(@"SOFTWARE\Exobo");
                //未注册 从新创建
                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(@"SOFTWARE\Exobo");
                //未注册 从新创建
                if (subKey == null)
                {
                    temp = false;
                }
                else
                {
                    temp = true;
                }
                subKey.Close();
                Key.Close();
                return temp;
            }
            catch (Exception)
            {
                throw;
            }

        }
      
posted on 2009-10-14 14:09  fffdc  阅读(630)  评论(0编辑  收藏  举报