C # obtain hardware information Through WMI

Source: Internet
Author: User

Sometimes you need to obtain hardware information and bind the user to log on.

 

The Code is as follows:

  1  private string GetProcessSerialNumber()  2         {  3             try  4             {  5                 ManagementObjectCollection ProcessCollection = new ManagementObjectSearcher(  6                       new System.Management.ManagementScope("\\\\localhost\\root\\cimv2"),  7                       new System.Management.ObjectQuery("SELECT * FROM Win32_processor")  8                       ).Get();  9                 if (ProcessCollection != null && ProcessCollection.Count > 0) 10                 { 11                     StringBuilder sb = new StringBuilder(); 12                     foreach (ManagementObject mo in ProcessCollection) 13                     { 14                         sb.Append(mo["ProcessorId"]); 15                     } 16                     return sb.ToString(); 17                 } 18                 else 19                 { 20                     return ""; 21                 } 22             } 23             catch (Exception) 24             {                 25                 return ""; 26             } 27         } 28  29         private string GetMotherBoardSerialNumber() 30         { 31             try 32             { 33                 ManagementObjectCollection ProcessCollection = new ManagementObjectSearcher( 34                       new System.Management.ManagementScope("\\\\localhost\\root\\cimv2"), 35                       new System.Management.ObjectQuery("SELECT * FROM Win32_bios") 36                       ).Get(); 37                 if (ProcessCollection != null && ProcessCollection.Count > 0) 38                 { 39                     StringBuilder sb = new StringBuilder(); 40                     foreach (ManagementObject mo in ProcessCollection) 41                     { 42                         sb.Append(mo["SerialNumber"]); 43                     } 44                     return sb.ToString(); 45                 } 46                 else 47                 { 48                     return ""; 49                 } 50             } 51             catch (Exception) 52             { 53                 return ""; 54             } 55         } 56  57         private string GetHardDisk0SerialNumber() 58         { 59             try 60             { 61                 ManagementObjectCollection ProcessCollection = new ManagementObjectSearcher( 62                       new System.Management.ManagementScope("\\\\localhost\\root\\cimv2"), 63                       new System.Management.ObjectQuery("SELECT * FROM Win32_DiskDrive") 64                       ).Get(); 65                 if (ProcessCollection != null && ProcessCollection.Count > 0) 66                 { 67                     StringBuilder sb = new StringBuilder(); 68                     foreach (ManagementObject mo in ProcessCollection) 69                     { 70                         if(mo["Name"].ToString().IndexOf("DRIVE0")!=-1) 71                         sb.Append(mo["SerialNumber"]); 72                     } 73                     return sb.ToString(); 74                 } 75                 else 76                 { 77                     return ""; 78                 } 79             } 80             catch (Exception) 81             { 82                 return ""; 83             } 84         } 85  86         private string GetNetWorkMACSerialNumber() 87         { 88             try 89             { 90                 ManagementObjectCollection ProcessCollection = new ManagementObjectSearcher( 91                       new System.Management.ManagementScope("\\\\localhost\\root\\cimv2"), 92                       new System.Management.ObjectQuery("SELECT * FROM Win32_NetworkAdapterConfiguration") 93                       ).Get(); 94                 if (ProcessCollection != null && ProcessCollection.Count > 0) 95                 { 96                     StringBuilder sb = new StringBuilder(); 97                     foreach (ManagementObject mo in ProcessCollection) 98                     { 99                         if (mo["IPEnabled"].ToString() == "True")100                             sb.Append(mo["MacAddress"]);101                     }102                     return sb.ToString();103                 }104                 else105                 {106                     return "";107                 }108             }109             catch (Exception)110             {111                 return "";112             }113         }

 

C # obtain hardware information Through WMI

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.