码迷,mamicode.com
首页 > Windows程序 > 详细

C#提供用户硬件唯一信息的辅助类 C#硬件信息查询类库

时间:2014-12-13 15:04:09      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   


C#提供用户硬件唯一信息的辅助类 C#硬件信息查询类库
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4  
  5 namespace  Commons
  6 {
  7     /// <summary>
  8     /// 提供用户硬件唯一信息的辅助类
  9     /// </summary>
 10     public class FingerprintHelper
 11     {
 12         public static string Value()
 13         {
 14             return pack(cpuId()
 15                     + biosId()
 16                     + diskId()
 17                     + baseId()
 18                     + videoId()
 19                     + macId());
 20         }
 21  
 22         //Return a hardware identifier
 23         private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
 24         {
 25             string result="";
 26             System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
 27             System.Management.ManagementObjectCollection moc = mc.GetInstances();
 28             foreach (System.Management.ManagementObject mo in moc)
 29             {
 30                 if (mo[wmiMustBeTrue].ToString()=="True")
 31                 {
 32  
 33                     //Only get the first one
 34                     if (result=="")
 35                     {
 36                         try
 37                         {
 38                             result = mo[wmiProperty].ToString();
 39                             break;
 40                         }
 41                         catch
 42                         {
 43                         }
 44                     }
 45  
 46                 }
 47             }
 48             return result;
 49         }
 50  
 51         //Return a hardware identifier
 52         private static string identifier(string wmiClass, string wmiProperty)
 53         {
 54             string result="";
 55             System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
 56             System.Management.ManagementObjectCollection moc = mc.GetInstances();
 57             foreach (System.Management.ManagementObject mo in moc)
 58             {
 59  
 60                 //Only get the first one
 61                 if (result=="")
 62                 {
 63                     try
 64                     {
 65                         result = mo[wmiProperty].ToString();
 66                         break;
 67                     }
 68                     catch
 69                     {
 70                     }
 71                 }
 72  
 73             }
 74             return result;
 75         }
 76  
 77         private static string cpuId()
 78         {
 79             //Uses first CPU identifier available in order of preference
 80             //Don‘t get all identifiers, as very time consuming
 81             string retVal = identifier("Win32_Processor", "UniqueId");
 82             if (retVal=="") //If no UniqueID, use ProcessorID
 83             {
 84                 retVal = identifier("Win32_Processor", "ProcessorId");
 85  
 86                 if (retVal=="") //If no ProcessorId, use Name
 87                 {
 88                     retVal = identifier("Win32_Processor", "Name");
 89  
 90  
 91                     if (retVal=="") //If no Name, use Manufacturer
 92                     {
 93                         retVal = identifier("Win32_Processor", "Manufacturer");
 94                     }
 95  
 96                     //Add clock speed for extra security
 97                     retVal +=identifier("Win32_Processor", "MaxClockSpeed");
 98                 }
 99             }
100  
101             return retVal;
102         }
103  
104         //BIOS Identifier
105         private static string biosId()
106         {
107             return identifier("Win32_BIOS", "Manufacturer")
108                     + identifier("Win32_BIOS", "SMBIOSBIOSVersion")
109                     + identifier("Win32_BIOS", "IdentificationCode")
110                     + identifier("Win32_BIOS", "SerialNumber")
111                     + identifier("Win32_BIOS", "ReleaseDate")
112                     + identifier("Win32_BIOS", "Version");
113         }
114  
115         //Main physical hard drive ID
116         private static string diskId()
117         {
118             return identifier("Win32_DiskDrive", "Model")
119                     + identifier("Win32_DiskDrive", "Manufacturer")
120                     + identifier("Win32_DiskDrive", "Signature")
121                     + identifier("Win32_DiskDrive", "TotalHeads");
122         }
123  
124         //Motherboard ID
125         private static string baseId()
126         {
127             return identifier("Win32_BaseBoard", "Model")
128                     + identifier("Win32_BaseBoard", "Manufacturer")
129                     + identifier("Win32_BaseBoard", "Name")
130                     + identifier("Win32_BaseBoard", "SerialNumber");
131         }
132  
133         //Primary video controller ID
134         private static string videoId()
135         {
136             return identifier("Win32_VideoController", "DriverVersion")
137                     + identifier("Win32_VideoController", "Name");
138         }
139  
140         //First enabled network card ID
141         private static string macId()
142         {
143             return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled");
144         }
145  
146         //Packs the string to 8 digits
147         private static string pack(string text)
148         {
149             string retVal;
150             int x = 0;
151             int y = 0;
152             foreach (char n in text)
153             {
154                 y++;
155                 x += (n*y);
156             }
157             retVal = x.ToString() + "00000000";
158  
159             return retVal.Substring(0, 8);
160         }
161     }
162 }

转自:记住你 C#社区

C#提供用户硬件唯一信息的辅助类 C#硬件信息查询类库

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/jizhuni/p/4161280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!