C # obtain the CPU serial number, MAC address, hard disk ID, local IP address, computer name, physical memory, PC type,

Source: Internet
Author: User

C # obtain the CPU serial number, MAC address, hard disk ID, local IP address, computer name, physical memory, PC type,

First introduce the service and then call

This article Reprinted 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. public static string LoginUserName; // 5. logon Username: public static string ComputerName; // 6. computer Name public static string SystemType; // 7. public static string TotalPhysicalMemory; // 8. memory usage unit: M static Computer () {CpuID = GetCpuID (); MacAddress = GetMacAddress (); DiskID = GetDiskID (); IpAddress = GetIPAddress (); LoginUserName = GetUserName (); systemType = GetSystemType (); TotalPhysicalMemory = GetTotalPhysicalMemory (); ComputerName = GetComputerName ();} // 1. code for obtaining the CPU serial number 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" ;}finally {}// 2. obtain the NIC 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" ;}finally {}// 3. obtain the hard disk 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. obtain the 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" ;}finally {}/// 5. logon username of the operating system static string GetUserName () {try {string un = ""; un = Environment. userName; return un;} catch {return "unknow";} finally {} // 6. obtain the 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 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" ;}finally {}}}

 

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.