First introduce the service and then call the
This paper is reproduced from http://blog.sina.com.cn/s/blog_7eeb43210101hf7f.html
public class Computer {public static string CpuID;//1.cpu serial number public static string MacAddress;//2. Mac serial number public static string DiskID; 3. Hard disk ID public static string ipaddress; 4.ip address public static string loginusername; 5. Login username public static string ComputerName; 6. Computer name public static string SystemType; 7. System type public static string TotalPhysicalMemory;
8. Unit of Stock: M static Computer () {CpuID = Getcpuid ();
MacAddress = Getmacaddress ();
DiskID = Getdiskid ();
IPAddress = Getipaddress ();
Loginusername = GetUserName ();
SystemType = Getsystemtype ();
TotalPhysicalMemory = Gettotalphysicalmemory ();
ComputerName = GetComputerName (); //1. Get CPU Serial number code static string Getcpuid () {try {string CpuInfo = "";//cpu serial number ManagemeNtclass mc = new ManagementClass ("Win32_Processor"); Managementobjectcollection MOC = MC.
GetInstances (); foreach (ManagementObject mo in moc) {cpuInfo = mo. properties["Processorid"].
Value.tostring ();
} MOC = null;
MC = null;
return cpuInfo;
Catch {return "Unknow";
The finally {}}//2. Get network card hardware address static string getmacaddress ()
{try {string mac = ' ";
ManagementClass mc = new ManagementClass ("Win32_NetworkAdapterConfiguration"); Managementobjectcollection MOC = MC.
GetInstances ();
foreach (ManagementObject mo in MoC) {if ((bool) mo["ipenabled"] = = True) {mac = mo["MacAddress"].
ToString ();
Break
} MOC = null;
MC = null;
return mac;
Catch {return "Unknow";
The finally {}}//3. Get Hard Drive ID static string Getdiskid ()
{try {String hdid = ' ";
ManagementClass mc = new ManagementClass ("win32_diskdrive"); Managementobjectcollection MOC = MC.
GetInstances (); foreach (ManagementObject mo in moc) {Hdid = (string) mo. properties["Model"].
Value;
} MOC = null;
MC = null;
return hdid;
Catch {return "Unknow";
} finally {}} 4. Get IP Address static string getipaddress () {try {string St
= "";
ManagementClass mc = new ManagementClass ("Win32_NetworkAdapterConfiguration"); Managementobjectcollection MOC = MC.
GetInstances ();
foreach (ManagementObject mo in MoC) {if ((bool) mo["ipenabled"] = = True) {//st=mo["IPAddress"].
ToString ();
System.Array ar; AR = (System.Array) (Mo. Properties["IPAddress"].
Value); st = AR. GetValue (0).
ToString ();
Break
} MOC = null;
MC = null;
Return St;
Catch {return "Unknow"; The finally {}}///5. The login of the operating systemHire user Name static string GetUserName () {try {string un = ' ";
UN = environment.username;
Return UN;
Catch {return "Unknow";
The finally {}}//6. Get computer name static string GetComputerName ()
{try {return System.Environment.MachineName;
Catch {return "Unknow";
} finally {}}///7 pc type static string Getsystemtype ()
{try {string st = ' ";
ManagementClass mc = new ManagementClass ("Win32_ComputerSystem"); Managementobjectcollection MOC = MC.
GetInstances ();
foreach (ManagementObject mo in MoC) { st = mo["SystemType"].
ToString ();
} MOC = null;
MC = null;
Return St;
Catch {return "Unknow"; } finally {}}///8. Physical memory static string Gettotalphys
Icalmemory () {try {string st = ' ";
ManagementClass mc = new ManagementClass ("Win32_ComputerSystem"); Managementobjectcollection MOC = MC.
GetInstances (); foreach (ManagementObject mo in moc) {st = mo["TotalPhysicalMemory"].
ToString ();
} MOC = null;
MC = null;
Return St;
Catch {return "Unknow"; } finally {}}}
When we use C # to develop a desktop program (Winform) program, we often need to get some information related to the system, such as user name, MAC address, IP address, hard disk ID, CPU serial number, system name, physical memory, etc.
First you need to introduce namespaces:
[CSharp] View Plain