开发C#中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息
小强我们在利用C#开发桌面程序(Winform)程序的时候,经常需要获取一些跟系统相关的信息,例如用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
| using System.Management;
public class GetSystemInfo { public static string GetUserName() { try { string strUserName = string.Empty; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { strUserName = mo["UserName"].ToString(); } moc = null; mc = null; return strUserName; } catch { return "unknown"; } } public static string GetMacAddress() { try { string strMac = string.Empty; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if ((bool)mo["IPEnabled"] == true) { strMac = mo["MacAddress"].ToString(); } } moc = null; mc = null; return strMac; } catch { return "unknown"; } } public static string getMacAddr_Local() { string madAddr = null; try { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc2 = mc.GetInstances(); foreach (ManagementObject mo in moc2) { if (Convert.ToBoolean(mo["IPEnabled"]) == true) { madAddr = mo["MacAddress"].ToString(); madAddr = madAddr.Replace(':', '-'); } mo.Dispose(); } if (madAddr == null) { return "unknown"; } else { return madAddr; } } catch (Exception) { return "unknown"; } } public static string GetClientLocalIPv6Address() { string strLocalIP = string.Empty; try { IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName()); IPAddress ipAddress = ipHost.AddressList[0]; strLocalIP = ipAddress.ToString(); return strLocalIP; } catch { return "unknown"; } } public static string GetClientLocalIPv4Address() { string strLocalIP = string.Empty; try { IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHost.AddressList[0]; strLocalIP = ipAddress.ToString(); return strLocalIP; } catch { return "unknown"; } } public static List<string> GetClientLocalIPv4AddressList() { List<string> ipAddressList = new List<string>(); try { IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName()); foreach (IPAddress ipAddress in ipHost.AddressList) { if (!ipAddressList.Contains(ipAddress.ToString())) { ipAddressList.Add(ipAddress.ToString()); } } } catch { } return ipAddressList; } public static string GetClientInternetIPAddress() { string strInternetIPAddress = string.Empty; try { using (WebClient webClient = new WebClient()) { strInternetIPAddress = webClient.DownloadString("http://www.coridc.com/ip"); Regex r = new Regex("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"); Match mth = r.Match(strInternetIPAddress); if (!mth.Success) { strInternetIPAddress = GetClientInternetIPAddress2(); mth = r.Match(strInternetIPAddress); if (!mth.Success) { strInternetIPAddress = "unknown"; } } return strInternetIPAddress; } } catch { return "unknown"; } } private static string GetClientInternetIPAddress2() { string tempip = ""; try { WebRequest wr = WebRequest.Create("http://iframe.ip138.com/ic.asp"); Stream s = wr.GetResponse().GetResponseStream(); StreamReader sr = new StreamReader(s, Encoding.Default); string all = sr.ReadToEnd(); int start = all.IndexOf("[") + 1; int end = all.IndexOf("]", start); tempip = all.Substring(start, end - start); sr.Close(); s.Close(); return tempip; } catch { return "unknown"; } } public static string GetDiskID() { try { string strDiskID = string.Empty; ManagementClass mc = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { strDiskID = mo.Properties["Model"].Value.ToString(); } moc = null; mc = null; return strDiskID; } catch { return "unknown"; } } public static string GetCpuID() { try { string strCpuID = string.Empty; ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { strCpuID = mo.Properties["ProcessorId"].Value.ToString(); } moc = null; mc = null; return strCpuID; } catch { return "unknown"; } } public static string GetSystemType() { try { string strSystemType = string.Empty; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { strSystemType = mo["SystemType"].ToString(); } moc = null; mc = null; return strSystemType; } catch { return "unknown"; } } public static string GetSystemName() { try { string strSystemName = string.Empty; ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT PartComponent FROM Win32_SystemOperatingSystem"); foreach (ManagementObject mo in mos.Get()) { strSystemName = mo["PartComponent"].ToString(); } mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT Caption FROM Win32_OperatingSystem"); foreach (ManagementObject mo in mos.Get()) { strSystemName = mo["Caption"].ToString(); } return strSystemName; } catch { return "unknown"; } } public static string GetTotalPhysicalMemory() { try { string strTotalPhysicalMemory = string.Empty; ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { strTotalPhysicalMemory = mo["TotalPhysicalMemory"].ToString(); } moc = null; mc = null; return strTotalPhysicalMemory; } catch { return "unknown"; } } public static string GetMotherBoardID() { try { ManagementClass mc = new ManagementClass("Win32_BaseBoard"); ManagementObjectCollection moc = mc.GetInstances(); string strID = null; foreach (ManagementObject mo in moc) { strID = mo.Properties["SerialNumber"].Value.ToString(); break; } return strID; } catch { return "unknown"; } } public static string GetAllUsersDesktopFolderPath() { RegistryKey folders; folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders"); string desktopPath = folders.GetValue("Common Desktop").ToString(); return desktopPath; }
public static string GetAllUsersStartupFolderPath() { RegistryKey folders; folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders"); string Startup = folders.GetValue("Common Startup").ToString(); return Startup; } private static RegistryKey OpenRegistryPath(RegistryKey root, string s) { s = s.Remove(0, 1) + @"/"; while (s.IndexOf(@"/") != -1) { root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"/"))); s = s.Remove(0, s.IndexOf(@"/") + 1); } return root; } private void Test() { RegistryKey folders; folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders"); string desktopPath = folders.GetValue("Common Desktop").ToString(); string fontsPath = folders.GetValue("Fonts").ToString(); string nethoodPath = folders.GetValue("Nethood").ToString(); string personalPath = folders.GetValue("Personal").ToString(); string programsPath = folders.GetValue("Programs").ToString(); string recentPath = folders.GetValue("Recent").ToString(); string sendtoPath = folders.GetValue("Sendto").ToString(); string startmenuPath = folders.GetValue("Startmenu").ToString(); string startupPath = folders.GetValue("Startup").ToString(); string favoritesPath = folders.GetValue("Favorites").ToString(); string historyPath = folders.GetValue("History").ToString(); string cookiesPath = folders.GetValue("Cookies").ToString(); string cachePath = folders.GetValue("Cache").ToString(); string appdataPath = folders.GetValue("Appdata").ToString(); string printhoodPath = folders.GetValue("Printhood").ToString(); }</span>
|