- //取得设备硬盘的卷标号
- 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;
- }
- //从数组随机抽取24个字符组成新的字符生成机器码
- private void Form1_Load(object sender, EventArgs e)
- {
- label1.Text = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号
- string[] strid = new string[24];
- for (int i = 0; i < 24; i++)//把字符赋给数组
- {
- strid[i] = label1.Text.Substring(i, 1);
- }
- label1.Text = "";
- Random rdid = new Random();
- for (int i = 0; i < 24; i++)//从数组随机抽取24个字符组成新的字符生成机器码
- {
- label1.Text += strid[rdid.Next(0, 24)];
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2807201513232.html
来源: http://www.codesnippet.cn/detail/2807201513232.html