Some CPUs do not support obtaining serial numbers.
strCpu = myObject.Properties[ "Processorid" ].Value.ToString(); when obtained, it is indicated that the CPU is not supported if the return is null.
Gets the collection object properties, unless 100% is determined, otherwise do not directly access the property. Once returned empty, a null reference error is reported
Code
Using system.management;//needs to add System.Management references in your project
Namespace Ecbc_cdkey
{
public class Softreg
{
<summary>
Get the volume label of the device's hard disk
</summary>
<returns></returns>
public string Getdiskvolumeserialnumber ()
{
ManagementClass mc = new ManagementClass ("Win32_NetworkAdapterConfiguration");
ManagementObject disk = new ManagementObject ("win32_logicaldisk.deviceid=\" C: \ "");
Disk. Get ();
return disk. GetPropertyValue ("VolumeSerialNumber"). ToString ();
}
<summary>
Get the serial number of the CPU
</summary>
<returns></returns>
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;
}
<summary>
Generate machine code
</summary>
<returns></returns>
public string Getmnum ()
{
String strnum = Getcpu () + getdiskvolumeserialnumber ()//Get 24-bit CPU and hard drive serial number
String strmnum = strnum.substring (0, 24);//Remove the first 24 characters from the generated string as machine code
return strmnum;
}
Public int[] Intcode = new int[127];//storage key
Public int[] Intnumber = ASCII value of the new int[25];//stored machine code
Public char[] CharCode = new char[25];//Storage machine code word
public void Setintcode ()//assign a number less than 10 to an array
{
for (int i = 1; i < intcode.length; i++)
{
Intcode[i] = i% 9;
}
}
<summary>
Generate registration Code
</summary>
<returns></returns>
public string Getrnum ()
{
Setintcode ();//Initialize 127-bit array
String mnum = This.getmnum ();//Get Registration code
for (int i = 1; i < charcode.length; i++)//store machine code in array
{
Charcode[i] = Convert.tochar (mnum.substring (i-1, 1));
}
for (int j = 1; j < Intnumber.length; J + +)//The ASCII value of the character is stored in an integer group.
{
INTNUMBER[J] = Intcode[convert.toint32 (charcode[j]) + Convert.ToInt32 (Charcode[j]);
}
String strasciiname = "";//For storing registration code
for (int j = 1; j < Intnumber.length; J + +)
{
if (Intnumber[j] >= && Intnumber[j] <= 57)//Determine if the ASCII value of the character is between 0-9
{
Strasciiname + = Convert.tochar (Intnumber[j]). ToString ();
}
else if (Intnumber[j] >= && intnumber[j] <= 90)//Determine if the ASCII value of the character is a-Z
{
Strasciiname + = Convert.tochar (Intnumber[j]). ToString ();
}
else if (Intnumber[j] >= && intnumber[j] <= 122)//Determine if the ASCII value of the character is a-Z
{
Strasciiname + = Convert.tochar (Intnumber[j]). ToString ();
}
else//character ASCII value is not in the above range
{
if (Intnumber[j] > 122)//Determine if the ASCII value of the character is greater than Z
{
Strasciiname + = Convert.tochar (Intnumber[j]-10). ToString ();
}
Else
{
Strasciiname + = Convert.tochar (Intnumber[j]-9). ToString ();
}
}
}
Return strasciiname;//returns the registration code
}
}
}
Content Source: http://blog.sina.com.cn/s/blog_645919c40100gryh.html
C # Read machine code, CPU serial number, generate registration code class (optimized)