using System;
using System.Management;
using System.Security.Cryptography;
using System.Text;
namespace Console4NetCore
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(GetComputerBit("A"));
}
//获取机器码
public static string GetComputerBit(string softname)
{
string bIOSSerialNumber = GetBIOSSerialNumber();
string hardDiskSerialNumber = GetHardDiskSerialNumber();
string netCardMACAddress = GetNetCardMACAddress();
string cpuID = GetCpuID();
var ComputerBit = bIOSSerialNumber + hardDiskSerialNumber + netCardMACAddress + cpuID;
MD5 md4 = new MD5CryptoServiceProvider();
ComputerBit = BitConverter.ToString(md4.ComputeHash(Encoding.Default.GetBytes(ComputerBit))).Replace("-", "").ToUpper().Substring(8, 0x10);
return softname + ComputerBit;
}
//CPUId
public static string GetCpuID()
{
try
{
ManagementObjectCollection instances = new ManagementClass("Win32_Processor").GetInstances();
string str = null;
foreach (ManagementObject obj2 in instances)
{
str = obj2.Properties["ProcessorId"].Value.ToString();
break;
}
return str;
}
catch
{
return "";
}
}
//硬盘序列号
public static string GetHardDiskSerialNumber()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_PhysicalMedia");
string str = "";
foreach (ManagementObject obj2 in searcher.Get())
{
str = obj2["SerialNumber"].ToString().Trim();
break;
}
return str;
}
catch
{
return "";
}
}
//MAC地址
public static string GetNetCardMACAddress()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT MACAddress FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))");
string str = "";
foreach (ManagementObject obj2 in searcher.Get())
{
str = obj2["MACAddress"].ToString().Trim();
}
return str;
}
catch
{
return "";
}
}
//BIOS序列号
public static string GetBIOSSerialNumber()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select SerialNumber From Win32_BIOS");
string str = "";
foreach (ManagementObject obj2 in searcher.Get())
{
str = obj2["SerialNumber"].ToString().Trim();
}
return str;
}
catch
{
return "";
}
}
}
}
C#生成机器码
最新推荐文章于 2024-04-07 16:33:35 发布