C # Get Computer hardware information (CPU, NIC, hard disk, memory, etc.)

Source: Internet
Author: User
Tags get ip

Using system;using system.collections.generic;using system.linq;using system.management;using System.Text;namespace formular{//<summary>//Author Zzh//////////////////////CPU (CP U serial number)///network card (MAC address)///hard drive (HDD ID)///System (System name, System model)///memory (memory size)///</summary> public class Comput        ER {///<summary>///CPU serial number///</summary> public string CpuID;        <summary>///LAN/mac address///</summary> public string MacAddress;        <summary>//HDD ID///</summary> public string DiskID;        <summary>////IP address///</summary> public string IpAddress;        <summary>///System login user name///</summary> public string loginusername;     <summary>///System name///</summary> public string ComputerName;   <summary>///System model///</summary> public string SystemType;         <summary>///physical memory (unit B)///</summary> public string totalphysicalmemory;        private static computer _instance;            public static computer Instance () {if (_instance = = null) _instance = new computer ();        return _instance;            } protected computer () {CpuID = Getcpuid ();            MacAddress = Getmacaddress ();            DiskID = Getdiskid ();            IpAddress = Getipaddress ();            Loginusername = GetUserName ();            SystemType = Getsystemtype ();            TotalPhysicalMemory = Gettotalphysicalmemory ();        ComputerName = GetComputerName (); } string Getcpuid () {try {//Get CPU Serial number code 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"; }} string Getmacaddress () {try {//Get NIC hardware address St                Ring 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"; }} string Getipaddress () {try {//Get IP address Strin                G 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"; }} string Getdiskid () {try {//Get Hard Drive ID string H                did = "";                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"; }}///<summary>///Operating system login username///</summary>//<returns></re                Turns> string GetUserName () {try {string st = "";                ManagementClass mc = new ManagementClass ("Win32_ComputerSystem"); Managementobjectcollection MOC = MC. GeTinstances (); foreach (ManagementObject mo in moc) {st = mo["UserName"].                 ToString ();                } MOC = null;                MC = null;            Return St;            } catch {return "Unknow"; }}///<summary>//PC Type///</summary>//<returns></returns&        Gt                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";   }      }///<summary>//physical Memory//</summary>//<returns></returns>                String gettotalphysicalmemory () {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"; }}///<summary>//System name///</summary>//<returns></returns&        Gt String GetComputerName () {try {return System.Environment.GetEnvironmentVaria            BLE ("ComputerName");   } catch         {return "Unknow"; }         }    }}
Need to introduce System.Management.dll


This class is used to record computer hardware information to prevent malicious damage


C # Get Computer hardware information (CPU, NIC, hard disk, memory, etc.)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.