using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; // using System.Management;//引用Net system.management.dll namespace WFrom { public partial class Form3 : Form { public int[] intCode = new int[127];//用于存密钥 public int[] intNumber = new int[25];//用于存机器码的Ascii值 public char[] Charcode = new char[25];//存储机器码字 public Form3() { InitializeComponent(); } //获得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; } txtCPU.Text = strCpu; return strCpu; } //获取设备硬盘的卷标号 public string GetDiskVolumeSerialNumber() { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=/"d:/""); disk.Get(); txt硬盘.Text = disk.GetPropertyValue("VolumeSerialNumber").ToString(); return disk.GetPropertyValue("VolumeSerialNumber").ToString(); } //生成机器码 private void button1_Click(object sender, EventArgs e) { txtCPU加硬盘.Text = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号 string[] strid = new string[24]; for (int i = 0; i < 24; i++) { strid[i] = txtCPU加硬盘.Text.Substring(i, 1);//把字符赋给数组 } txtCPU加硬盘.Text = ""; Random rdid = new Random(); for (int i = 0; i < 24; i++) { txtCPU加硬盘.Text += strid[rdid.Next(0, 24)];//从数组随机抽取24个字符组成新的字符生成机器码 } } //使用机器码生成注册码 public void setIntCode()//给数组赋值个小于10的随机数 { Random ra = new Random(); for (int i = 1; i < intCode.Length; i++) { intCode[i] = ra.Next(0, 9); } } //生成注册码 private void button2_Click(object sender, EventArgs e) { if (txtCPU加硬盘.Text != "") { //把机器码存入数组中 setIntCode();//初始化127位数组 for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中 { Charcode[i] = Convert.ToChar(txtCPU加硬盘.Text.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 = null;//用于存储机器码 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(); } } txt生成注册码.Text = strAsciiName;//得到注册码 } } else { MessageBox.Show("请选生成机器码", "注册提示"); } } //用户输入注册码注册软件 private void button3_Click(object sender, EventArgs e) { if (txtCPU加硬盘.Text != "") { if (txt注册.Text.TrimEnd().Equals(txtCPU加硬盘.Text.TrimEnd())) { Microsoft.Win32.RegistryKey retkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("software", true).CreateSubKey("ZHY").CreateSubKey("ZHY.INI").CreateSubKey(txt注册.Text.TrimEnd()); retkey.SetValue("UserName", "MySoft"); MessageBox.Show("注册成功"); } else { MessageBox.Show("注册码输入错误"); } } else { MessageBox.Show("请生成注册码", "注册提示"); } } private void btn清空_Click(object sender, EventArgs e) { txtCPU.Text = ""; txt硬盘.Text = ""; txtCPU加硬盘.Text = ""; txt生成注册码.Text = ""; txt注册.Text = ""; } } } namespace WFrom { partial class Form3 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form3)); this.button1 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.button2 = new System.Windows.Forms.Button(); this.label3 = new System.Windows.Forms.Label(); this.button3 = new System.Windows.Forms.Button(); this.txt注册 = new System.Windows.Forms.TextBox(); this.txtCPU = new System.Windows.Forms.TextBox(); this.txt硬盘 = new System.Windows.Forms.TextBox(); this.txtCPU加硬盘 = new System.Windows.Forms.TextBox(); this.btn清空 = new System.Windows.Forms.Button(); this.label4 = new System.Windows.Forms.Label(); this.txt生成注册码 = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.groupBox3.SuspendLayout(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(309, 30); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(100, 23); this.button1.TabIndex = 0; this.button1.Text = "生成机器码"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 29); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(59, 12); this.label1.TabIndex = 1; this.label1.Text = "CPU序列号"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(6, 57); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(65, 12); this.label2.TabIndex = 2; this.label2.Text = "硬盘卷标号"; // // button2 // this.button2.Location = new System.Drawing.Point(309, 146); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(100, 23); this.button2.TabIndex = 3; this.button2.Text = "生成注册码"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(18, 84); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(53, 12); this.label3.TabIndex = 4; this.label3.Text = "CPU+硬盘"; // // button3 // this.button3.Location = new System.Drawing.Point(309, 209); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(100, 23); this.button3.TabIndex = 5; this.button3.Text = "输入注册码注册"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // txt注册 // this.txt注册.Location = new System.Drawing.Point(77, 20); this.txt注册.Name = "txt注册"; this.txt注册.Size = new System.Drawing.Size(208, 21); this.txt注册.TabIndex = 6; // // txtCPU // this.txtCPU.Location = new System.Drawing.Point(77, 20); this.txtCPU.Name = "txtCPU"; this.txtCPU.Size = new System.Drawing.Size(208, 21); this.txtCPU.TabIndex = 7; // // txt硬盘 // this.txt硬盘.Location = new System.Drawing.Point(77, 48); this.txt硬盘.Name = "txt硬盘"; this.txt硬盘.Size = new System.Drawing.Size(208, 21); this.txt硬盘.TabIndex = 8; // // txtCPU加硬盘 // this.txtCPU加硬盘.Location = new System.Drawing.Point(77, 75); this.txtCPU加硬盘.Name = "txtCPU加硬盘"; this.txtCPU加硬盘.Size = new System.Drawing.Size(208, 21); this.txtCPU加硬盘.TabIndex = 9; // // btn清空 // this.btn清空.Location = new System.Drawing.Point(309, 269); this.btn清空.Name = "btn清空"; this.btn清空.Size = new System.Drawing.Size(100, 23); this.btn清空.TabIndex = 10; this.btn清空.Text = "清空"; this.btn清空.UseVisualStyleBackColor = true; this.btn清空.Click += new System.EventHandler(this.btn清空_Click); // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(6, 29); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(65, 12); this.label4.TabIndex = 11; this.label4.Text = "生产注册码"; // // txt生成注册码 // this.txt生成注册码.Location = new System.Drawing.Point(77, 20); this.txt生成注册码.Name = "txt生成注册码"; this.txt生成注册码.Size = new System.Drawing.Size(208, 21); this.txt生成注册码.TabIndex = 12; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(42, 29); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(29, 12); this.label5.TabIndex = 13; this.label5.Text = "注册"; // // groupBox1 // this.groupBox1.Controls.Add(this.txtCPU); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.txt硬盘); this.groupBox1.Controls.Add(this.txtCPU加硬盘); this.groupBox1.Location = new System.Drawing.Point(8, 12); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(295, 110); this.groupBox1.TabIndex = 14; this.groupBox1.TabStop = false; this.groupBox1.Text = "步骤1"; // // groupBox2 // this.groupBox2.Controls.Add(this.label4); this.groupBox2.Controls.Add(this.txt生成注册码); this.groupBox2.Location = new System.Drawing.Point(8, 128); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(295, 57); this.groupBox2.TabIndex = 15; this.groupBox2.TabStop = false; this.groupBox2.Text = "步骤2"; // // groupBox3 // this.groupBox3.Controls.Add(this.txt注册); this.groupBox3.Controls.Add(this.label5); this.groupBox3.Location = new System.Drawing.Point(8, 191); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(295, 58); this.groupBox3.TabIndex = 15; this.groupBox3.TabStop = false; this.groupBox3.Text = "步骤3"; // // Form3 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(441, 304); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox3); this.Controls.Add(this.groupBox1); this.Controls.Add(this.btn清空); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "Form3"; this.Text = "Form3"; this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.groupBox2.ResumeLayout(false); this.groupBox2.PerformLayout(); this.groupBox3.ResumeLayout(false); this.groupBox3.PerformLayout(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button button2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Button button3; private System.Windows.Forms.TextBox txt注册; private System.Windows.Forms.TextBox txtCPU; private System.Windows.Forms.TextBox txt硬盘; private System.Windows.Forms.TextBox txtCPU加硬盘; private System.Windows.Forms.Button btn清空; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox txt生成注册码; private System.Windows.Forms.Label label5; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.GroupBox groupBox3; } }