C # Get native CPU serial number, MAC address, hard disk ID, native IP address, computer name, physical memory, PC type

Source: Internet
Author: User
Tags get ip

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

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.