标题:操作注册表时遇到的问题
只看楼主
growingbird
Rank: 1
等 级:新手上路
帖 子:68
专家分:5
注 册:2008-12-24
结帖率:0
 问题点数:0 回复次数:1 
操作注册表时遇到的问题
我新建了一个类 userclass\SoftReg.cs ;专门用来获取硬盘和CPU 的机器码
///////////////////////邪恶的分割线////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

namespace ERP.userclass
{
    class SoftReg
    {
        //// 取得设备硬盘的卷标号
        public string GetDiskVolumeSerialNumber()
        {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"d:\"");
            disk.Get();
            return disk.GetPropertyValue("VolumeSerialNumber").ToString();
        }

        //获得CPU的序列号
        public string getCpu()
        {
            string strCpu = null;
            ManagementClass myCpu = new ManagementClass("win32_Processor");
            ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
            foreach (ManagementObject myObject in myCpuConnection)
            {
                strCpu = myObject.Properties["Processorid"].Value.ToString();
                break;
            }
            return strCpu;
        }


        //由CPU和硬盘码来生成机器码
        //生成机器码
        public string getMNum()
        {
            string strNum = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号

            //string strNum = getCpu();//获得24位Cpu和硬盘序列号
            string strMNum = strNum.Substring(0,24);//从生成的字符串中取出前24个字符做为机器码
            return strMNum;
        }

        public int[] intCode = new int[127];//存储密钥
        public int[] intNumber = new int[25];//存机器码的Ascii值
        public char[] Charcode = new char[25];//存储机器码字

        public void setIntCode()//给数组赋值小于10的数
        {
            for (int i = 1; i < intCode.Length; i++)
            {
                intCode[i] = i % 9;
            }
        }

        //生成注册码
        public string getRNum()
        {
            setIntCode();//初始化127位数组
            for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中
            {
                Charcode[i] = Convert.ToChar(this.getMNum().Substring(i - 1, 1));
            }
            for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。
            {
                intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);
            }
            string strAsciiName = "";//用于存储注册码
            for (int j = 1; j < intNumber.Length; j++)
            {
                if (intNumber[j] >= 48 && intNumber[j] <= 57)//判断字符ASCII值是否0-9之间
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判断字符ASCII值是否A-Z之间
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判断字符ASCII值是否a-z之间
                {
                    strAsciiName += Convert.ToChar(intNumber[j]).ToString();
                }
                else//判断字符ASCII值不在以上范围内
                {
                    if (intNumber[j] > 122)//判断字符ASCII值是否大于z
                    {
                        strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString();
                    }
                    else
                    {
                        strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
                    }
                }
            }
            return strAsciiName;
        }

    }
}
///////////////////////邪恶的分割线////////////////////////////////

///////////////////////在的主窗体加载的时候来判断系统是否注册////////////////////////////////


 private void ERP_Load(object sender, EventArgs e)
    {
       //打开注册表项

      RegistryKey retkey = Microsoft.Win32.Registry.CurrentConfig.OpenSubKey("Software", true).CreateSubKey("wxk").CreateSubKey("wxk.INI");

      //判断是否注册
      foreach (string strRNum in retkey.GetSubKeyNames())
      {
          if (strRNum.Equals(softreg.getRNum()))
          {
              this.Text = "管理系统(已注册)";
              this.WindowState = FormWindowState.Maximized;

              //toolStripStatusLabel1.Text = passname.usertype+": "+passname.nameinfo.Trim()+"       "+"欢迎您使管理系统!";
              toolStripStatusLabel3.Text = passname.usertype + ": " + passname.nameinfo.Trim() + "       " + "欢迎您使用管理系统!";
              this.timer1.Start();

                          //注册软件按钮不显示
              this.menuStrip1.Items[3].Visible = false;

              return;
          }
      }

      this.Text = "管理系统(未注册)";

////////////////没有注册的话用户就点注册软件按钮来注册/////////////////////////////

    //注册
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text=="")           //判断是否输入了注册码
            {
                MessageBox.Show("注册码输入不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                //用户输入的注册码等于系统自己计算出来的注册码。那么注册成功
                if (textBox1.Text.Equals(softreg.getRNum()))
                {
                    RegistryKey retkey = Microsoft.Win32.Registry.CurrentConfig.OpenSubKey("Software", true).CreateSubKey("wxk").CreateSubKey("wxk.INI").CreateSubKey(textBox1.Text.Trim());

                    retkey.SetValue("UserName","ADSOFT");
                  
                    retkey.Close(); //修改完注册表后关闭掉,并刷新存盘
                    MessageBox.Show("注册成功,程序需要重新加载!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //this.Hide();
                    this.Close();
                    //frmMain frmmain = new frmMain();
                    //frmmain.Show();
                }
                else
                {
                    MessageBox.Show("注册码输入错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            

        }


现在代码执行都是可以完成的,现在的问题就是当用户没有注册时,点注册的时候,输入注册码之后,大约要30秒的时间才显示注册成功

注册成功后,每次主窗体运行时会检查注册表,看看是否被注册,此时耗时也大约有30多秒

我想请教下各位大拿,这个究竟是怎么回事啊,程序慢在了那个地方啊???

搜索更多相关主题的帖子: 注册表 
2010-11-08 13:29
growingbird
Rank: 1
等 级:新手上路
帖 子:68
专家分:5
注 册:2008-12-24
得分:0 
那位高手指导下啊
2010-11-08 17:57



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-325291-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.106345 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved