First, the service is introduced and then called
This article is reproduced from http://blog.sina.com.cn/s/blog_7eeb43210101hf7f.html
Public classComputer { Public Static stringCpuID;//1.cpu Serial Number Public Static stringMacAddress;//2.mac Serial Number Public Static stringDiskID;//3. Hard Drive ID Public Static stringIpAddress;//4.ip Address Public Static stringLoginusername;//5. Login User name Public Static stringComputerName;//6. Computer name Public Static stringSystemType;//7. System Type Public Static stringTotalPhysicalMemory;//8. Unit of Memory: M Staticcomputer () {CpuID=Getcpuid (); MacAddress=getmacaddress (); DiskID=Getdiskid (); IpAddress=getipaddress (); Loginusername=GetUserName (); SystemType=Getsystemtype (); TotalPhysicalMemory=gettotalphysicalmemory (); ComputerName=GetComputerName (); } //1. Get the CPU serial number code Static stringGetcpuid () {Try { stringCpuInfo ="";//CPU Serial NumberManagementClass MC =NewManagementClass ("Win32_Processor"); Managementobjectcollection MOC=MC. GetInstances (); foreach(ManagementObject moinchMOC) {CpuInfo= mo. properties["Processorid"]. Value.tostring (); } MOC=NULL; MC=NULL; returnCpuInfo; } Catch { return "Unknow"; } finally { } } //2. Get the NIC hardware address Static stringgetmacaddress () {Try { stringMac =""; ManagementClass MC=NewManagementClass ("Win32_NetworkAdapterConfiguration"); Managementobjectcollection MOC=MC. GetInstances (); foreach(ManagementObject moinchMOC) { if((BOOL) mo["ipenabled"] ==true) {mac= mo["MacAddress"]. ToString (); Break; }} MOC=NULL; MC=NULL; returnmac; } Catch { return "Unknow"; } finally { } } //3. Get the hard drive ID Static stringGetdiskid () {Try{String HDid=""; ManagementClass MC=NewManagementClass ("win32_diskdrive"); Managementobjectcollection MOC=MC. GetInstances (); foreach(ManagementObject moinchMOC) {HDid= (string) mo. properties["Model"]. Value; } MOC=NULL; MC=NULL; returnHDid; } Catch { return "Unknow"; } finally { } } //4. Get the IP address Static stringgetipaddress () {Try { stringSt =""; ManagementClass MC=NewManagementClass ("Win32_NetworkAdapterConfiguration"); Managementobjectcollection MOC=MC. GetInstances (); foreach(ManagementObject moinchMOC) { 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; returnSt; } Catch { return "Unknow"; } finally { } } ///5. Login user name of the operating system Static stringGetUserName () {Try { stringUN =""; Un=Environment.username; returnun; } Catch { return "Unknow"; } finally { } } //6. Get the computer name Static stringGetComputerName () {Try { returnSystem.Environment.MachineName; } Catch { return "Unknow"; } finally { } } ///7 PC Type Static stringGetsystemtype () {Try { stringSt =""; ManagementClass MC=NewManagementClass ("Win32_ComputerSystem"); Managementobjectcollection MOC=MC. GetInstances (); foreach(ManagementObject moinchMOC) {St= mo["SystemType"]. ToString (); } MOC=NULL; MC=NULL; returnSt; } Catch { return "Unknow"; } finally { } } ///8. Physical Memory Static stringgettotalphysicalmemory () {Try { stringSt =""; ManagementClass MC=NewManagementClass ("Win32_ComputerSystem"); Managementobjectcollection MOC=MC. GetInstances (); foreach(ManagementObject moinchMOC) {St= mo["TotalPhysicalMemory"]. ToString (); } MOC=NULL; MC=NULL; returnSt; } Catch { return "Unknow"; } finally { } } }
C # Gets the native CPU serial number, MAC address, hard drive ID, native IP address, computer name, physical memory, PC type