C# 獲取計算機惟一標識

 1         private static string _sFingerPrint { get; set; }  2 
 3         /// <summary>
 4         /// 計算機惟一標識  5         /// </summary>
 6         public static string sFingerPrint  7  {  8             get
 9  {  10                 if (string.IsNullOrEmpty(_sFingerPrint))  11  {  12                     _sFingerPrint = GetHash("UUID >> " + UUID() + "\r\nCPU >> " + CpuId() + "\r\nBIOS >> " +
 13                     BiosId() + "\r\nBASE >> " + BaseId() + "\r\nDisk >> " + DiskId() + "\r\nVideo >> " +
 14                     VideoId() + "\r\nMAC >> " + MacId());  15  }  16                 return _sFingerPrint;  17  }  18  }  19 
 20         /// <summary>
 21         /// 獲取硬件標識符  22         /// </summary>
 23         /// <param name="wmiClass"></param>
 24         /// <param name="wmiProperty"></param>
 25         /// <param name="wmiMustBeTrue"></param>
 26         /// <returns></returns>
 27         private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)  28  {  29             string result = "";  30             System.Management.ManagementClass mc =
 31         new System.Management.ManagementClass(wmiClass);  32             System.Management.ManagementObjectCollection moc = mc.GetInstances();  33             foreach (System.Management.ManagementObject mo in moc)  34  {  35                 if (mo[wmiMustBeTrue].ToString() == "True")  36  {  37                     //Only get the first one
 38                     if (result == "")  39  {  40                         try
 41  {  42                             result = mo[wmiProperty].ToString();  43                             break;  44  }  45                         catch
 46  {  47  }  48  }  49  }  50  }  51             return result;  52  }  53         /// <summary>
 54         /// 獲取硬件標識符  55         /// </summary>
 56         /// <param name="wmiClass"></param>
 57         /// <param name="wmiProperty"></param>
 58         /// <returns></returns>
 59         private static string identifier(string wmiClass, string wmiProperty)  60  {  61             string result = "";  62             System.Management.ManagementClass mc =
 63         new System.Management.ManagementClass(wmiClass);  64             System.Management.ManagementObjectCollection moc = mc.GetInstances();  65             foreach (System.Management.ManagementObject mo in moc)  66  {  67                 //Only get the first one
 68                 if (result == "")  69  {  70                     try
 71  {  72                         result = mo[wmiProperty].ToString();  73                         break;  74  }  75                     catch
 76  {  77  }  78  }  79  }  80             return result;  81  }  82 
 83         /// <summary>
 84         /// 獲取UUID  85         /// </summary>
 86         /// <returns></returns>
 87         public static string UUID()  88  {  89             string code = null;  90             SelectQuery query = new SelectQuery("select * from Win32_ComputerSystemProduct");  91             using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))  92  {  93                 foreach (var item in searcher.Get())  94  {  95                     using (item)  96                         code = item["UUID"].ToString();  97  }  98  }  99             return code; 100  } 101 
102         /// <summary>
103         /// 獲取CPUID 104         /// </summary>
105         /// <returns></returns>
106         public static string CpuId() 107  { 108             //Uses first CPU identifier available in order of preference 109             //Don't get all identifiers, as it is very time consuming
110             string retVal = identifier("Win32_Processor", "UniqueId"); 111             if (retVal == "") //If no UniqueID, use ProcessorID
112  { 113                 retVal = identifier("Win32_Processor", "ProcessorId"); 114                 if (retVal == "") //If no ProcessorId, use Name
115  { 116                     retVal = identifier("Win32_Processor", "Name"); 117                     if (retVal == "") //If no Name, use Manufacturer
118  { 119                         retVal = identifier("Win32_Processor", "Manufacturer"); 120  } 121                     //Add clock speed for extra security
122                     retVal += identifier("Win32_Processor", "MaxClockSpeed"); 123  } 124  } 125             return retVal; 126  } 127         /// <summary>
128         /// 獲取BIOSID 129         /// </summary>
130         /// <returns></returns>
131         public static string BiosId() 132  { 133             return identifier("Win32_BIOS", "Manufacturer") 134             + identifier("Win32_BIOS", "SMBIOSBIOSVersion") 135             + identifier("Win32_BIOS", "IdentificationCode") 136             + identifier("Win32_BIOS", "SerialNumber") 137             + identifier("Win32_BIOS", "ReleaseDate") 138             + identifier("Win32_BIOS", "Version"); 139  } 140         /// <summary>
141         /// 獲取硬盤ID 142         /// </summary>
143         /// <returns></returns>
144         public static string DiskId() 145  { 146             return identifier("Win32_DiskDrive", "Model") 147             + identifier("Win32_DiskDrive", "Manufacturer") 148             + identifier("Win32_DiskDrive", "Signature") 149             + identifier("Win32_DiskDrive", "TotalHeads"); 150  } 151         /// <summary>
152         /// 獲取主板ID 153         /// </summary>
154         /// <returns></returns>
155         public static string BaseId() 156  { 157             return identifier("Win32_BaseBoard", "Model") 158             + identifier("Win32_BaseBoard", "Manufacturer") 159             + identifier("Win32_BaseBoard", "Name") 160             + identifier("Win32_BaseBoard", "SerialNumber"); 161  } 162         /// <summary>
163         /// 獲取主視頻控制器ID 164         /// </summary>
165         /// <returns></returns>
166         public static string VideoId() 167  { 168             return identifier("Win32_VideoController", "DriverVersion") 169             + identifier("Win32_VideoController", "Name"); 170  } 171         /// <summary>
172         /// 獲取網卡ID 173         /// </summary>
174         /// <returns></returns>
175         public static string MacId() 176  { 177             return identifier("Win32_NetworkAdapterConfiguration", 178                 "MACAddress", "IPEnabled"); 179         }
相關文章
相關標籤/搜索