1 // 註意:首先要在項目中添加引用 System.Management 2 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Web; 7 using System.Ma
1 // 註意:首先要在項目中添加引用 System.Management 2 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Web; 7 using System.Management; 8 9 public class Computer 10 { 11 public string CpuID; //CPU的ID 12 public int CpuCount; //CPU的個數 13 public string[] CpuMHZ;//CPU頻率 單位:hz 14 public string MacAddress;//電腦的MAC地址 15 public string DiskID;//硬碟的ID 16 public string DiskSize;//硬碟大小 單位:bytes 17 public string IpAddress;//電腦的IP地址 18 public string LoginUserName;//操作系統登錄用戶名 19 public string ComputerName;//電腦名 20 public string SystemType;//系統類型 21 public string TotalPhysicalMemory; //總共的記憶體 單位:M 22 public string BoardID;// 主板序列號 23 public string BoardType;// 主板型號 24 private static Computer _instance; 25 public static Computer Instance() 26 { 27 if (_instance == null) 28 _instance = new Computer(); 29 return _instance; 30 } 31 public Computer() 32 { 33 CpuID = GetCpuID(); 34 CpuCount = GetCpuCount(); 35 CpuMHZ = GetCpuMHZ(); 36 MacAddress = GetMacAddress(); 37 DiskID = GetDiskID(); 38 DiskSize = GetSizeOfDisk(); 39 IpAddress = GetIPAddress(); 40 LoginUserName = GetUserName(); 41 SystemType = GetSystemType(); 42 TotalPhysicalMemory = GetTotalPhysicalMemory(); 43 ComputerName = GetComputerName(); 44 BoardID = GetBoardID(); 45 BoardType = GetBoardType(); 46 } 47 string GetCpuID() 48 { 49 try 50 { 51 //獲取CPU序列號代碼 52 string cpuInfo = " ";//cpu序列號 53 ManagementClass mc = new ManagementClass("Win32_Processor"); 54 ManagementObjectCollection moc = mc.GetInstances(); 55 foreach (ManagementObject mo in moc) 56 { 57 cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); 58 } 59 moc = null; 60 mc = null; 61 return cpuInfo; 62 } 63 catch 64 { 65 return "unknow "; 66 } 67 finally 68 { 69 } 70 71 } 72 public static int GetCpuCount() 73 { 74 try 75 { 76 using (ManagementClass mCpu = new ManagementClass("Win32_Processor")) 77 { 78 ManagementObjectCollection cpus = mCpu.GetInstances(); 79 return cpus.Count; 80 } 81 } 82 catch 83 { 84 } 85 return -1; 86 } 87 public static string[] GetCpuMHZ() 88 { 89 ManagementClass mc = new ManagementClass("Win32_Processor"); 90 ManagementObjectCollection cpus = mc.GetInstances(); 91 92 string[] mHz = new string[cpus.Count]; 93 int c = 0; 94 ManagementObjectSearcher mySearch = new ManagementObjectSearcher("select * from Win32_Processor"); 95 foreach (ManagementObject mo in mySearch.Get()) 96 { 97 mHz[c] = mo.Properties["CurrentClockSpeed"].Value.ToString(); 98 c++; 99 } 100 mc.Dispose(); 101 mySearch.Dispose(); 102 return mHz; 103 } 104 public static string GetSizeOfDisk() 105 { 106 ManagementClass mc = new ManagementClass("Win32_DiskDrive"); 107 ManagementObjectCollection moj = mc.GetInstances(); 108 foreach (ManagementObject m in moj) 109 { 110 return m.Properties["Size"].Value.ToString(); 111 } 112 return "-1"; 113 } 114 string GetMacAddress() 115 { 116 try 117 { 118 //獲取網卡硬體地址 119 string mac = " "; 120 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 121 ManagementObjectCollection moc = mc.GetInstances(); 122 foreach (ManagementObject mo in moc) 123 { 124 if ((bool)mo["IPEnabled"] == true) 125 { 126 mac = mo["MacAddress"].ToString(); 127 break; 128 } 129 } 130 moc = null; 131 mc = null; 132 return mac; 133 } 134 catch 135 { 136 return "unknow "; 137 } 138 finally 139 { 140 } 141 142 } 143 string GetIPAddress() 144 { 145 try 146 { 147 //獲取IP地址 148 string st = " "; 149 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 150 ManagementObjectCollection moc = mc.GetInstances(); 151 foreach (ManagementObject mo in moc) 152 { 153 if ((bool)mo["IPEnabled"] == true) 154 { 155 //st=mo[ "IpAddress "].ToString(); 156 System.Array ar; 157 ar = (System.Array)(mo.Properties["IpAddress"].Value); 158 st = ar.GetValue(0).ToString(); 159 break; 160 } 161 } 162 moc = null; 163 mc = null; 164 return st; 165 } 166 catch 167 { 168 return "unknow "; 169 } 170 finally 171 { 172 } 173 174 } 175 string GetDiskID() 176 { 177 try 178 { 179 //獲取硬碟ID 180 String HDid = " "; 181 ManagementClass mc = new ManagementClass("Win32_DiskDrive"); 182 ManagementObjectCollection moc = mc.GetInstances(); 183 foreach (ManagementObject mo in moc) 184 { 185 HDid = (string)mo.Properties["Model"].Value; 186 } 187 moc = null; 188 mc = null; 189 return HDid; 190 } 191 catch 192 { 193 return "unknow "; 194 } 195 finally 196 { 197 } 198 199 } 200 /// <summary> 201 /// 操作系統的登錄用戶名 202 /// </summary> 203 /// <returns> </returns> 204 string GetUserName() 205 { 206 try 207 { 208 string st = " "; 209 ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); 210 ManagementObjectCollection moc = mc.GetInstances(); 211 foreach (ManagementObject mo in moc) 212 { 213 214 st = mo["UserName"].ToString(); 215 216 } 217 moc = null; 218 mc = null; 219 return st; 220 } 221 catch 222 { 223 return "unknow "; 224 } 225 finally 226 { 227 } 228 229 } 230 string GetSystemType() 231 { 232 try 233 { 234 string st = " "; 235 ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); 236 ManagementObjectCollection moc = mc.GetInstances(); 237 foreach (ManagementObject mo in moc) 238 { 239 240 st = mo["SystemType"].ToString(); 241 242 } 243 moc = null; 244 mc = null; 245 return st; 246 } 247 catch 248 { 249 return "unknow "; 250 } 251 finally 252 { 253 } 254 255 } 256 string GetTotalPhysicalMemory() 257 { 258 try 259 { 260 261 string st = " "; 262 ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); 263 ManagementObjectCollection moc = mc.GetInstances(); 264 foreach (ManagementObject mo in moc) 265 { 266 267 st = mo["TotalPhysicalMemory"].ToString(); 268 269 } 270 moc = null; 271 mc = null; 272 return st; 273 } 274 catch 275 { 276 return "unknow "; 277 } 278 finally 279 { 280 } 281 } 282 string GetComputerName() 283 { 284 try 285 { 286 return System.Environment.GetEnvironmentVariable("ComputerName"); 287 } 288 catch 289 { 290 return "unknow "; 291 } 292 finally 293 { 294 } 295 } 296 297 string GetBoardID() 298 { 299 string strbNumber = string.Empty; 300 try 301 { 302 ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_baseboard"); 303 foreach (ManagementObject mo in mos.Get()) 304 { 305 strbNumber = mo["SerialNumber"].ToString().Trim('.'); 306 break; 307 } 308 } 309 catch (Exception ex) 310 { 311 strbNumber = "unknow"; 312 } 313 314 return strbNumber; 315 } 316 317 /// <summary> 318 /// 主板型號 319 /// </summary> 320 /// <returns></returns> 321 string GetBoardType() 322 { 323 string st = ""; 324 ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_BaseBoard"); 325 foreach (ManagementObject mo in mos.Get()) 326 { 327 st = mo["Product"].ToString(); 328 } 329 return st; 330 } 331 332 333 }