ComputerInfo.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Net.NetworkInformation;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace PacsView3D
  10. {
  11. public class ComputerInfo
  12. {
  13. public static string GetComputerInfo()
  14. {
  15. string info = string.Empty;
  16. string cpu = GetCPUInfo();
  17. string baseBoard = GetBaseBoardInfo();
  18. string bios = GetBIOSInfo();
  19. string mac = GetMACInfo();
  20. info = string.Concat(cpu, baseBoard, bios, mac);
  21. return info;
  22. }
  23. private static string GetCPUInfo()
  24. {
  25. string info = string.Empty;
  26. info = GetHardWareInfo("Win32_Processor", "ProcessorId");
  27. return info;
  28. }
  29. private static string GetBIOSInfo()
  30. {
  31. string info = string.Empty;
  32. info = GetHardWareInfo("Win32_BIOS", "SerialNumber");
  33. return info;
  34. }
  35. private static string GetBaseBoardInfo()
  36. {
  37. string info = string.Empty;
  38. info = GetHardWareInfo("Win32_BaseBoard", "SerialNumber");
  39. return info;
  40. }
  41. private static string GetMACInfo()
  42. {
  43. string info = string.Empty;
  44. info = GetHardWareInfo("Win32_BaseBoard", "SerialNumber");
  45. return info;
  46. }
  47. private static string GetHardWareInfo(string typePath, string key)
  48. {
  49. try
  50. {
  51. ManagementClass managementClass = new ManagementClass(typePath);
  52. ManagementObjectCollection mn = managementClass.GetInstances();
  53. PropertyDataCollection properties = managementClass.Properties;
  54. foreach (PropertyData property in properties)
  55. {
  56. if (property.Name == key)
  57. {
  58. foreach (ManagementObject m in mn)
  59. {
  60. return m.Properties[property.Name].Value.ToString();
  61. }
  62. }
  63. }
  64. }
  65. catch (Exception ex)
  66. {
  67. //这里写异常的处理
  68. }
  69. return string.Empty;
  70. }
  71. private static string GetMacAddressByNetworkInformation()
  72. {
  73. string key = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\";
  74. string macAddress = string.Empty;
  75. try
  76. {
  77. NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  78. foreach (NetworkInterface adapter in nics)
  79. {
  80. if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet
  81. && adapter.GetPhysicalAddress().ToString().Length != 0)
  82. {
  83. string fRegistryKey = key + adapter.Id + "\\Connection";
  84. RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
  85. if (rk != null)
  86. {
  87. string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
  88. int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
  89. if (fPnpInstanceID.Length > 3 &&
  90. fPnpInstanceID.Substring(0, 3) == "PCI")
  91. {
  92. macAddress = adapter.GetPhysicalAddress().ToString();
  93. for (int i = 1; i < 6; i++)
  94. {
  95. macAddress = macAddress.Insert(3 * i - 1, ":");
  96. }
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. //这里写异常的处理
  106. }
  107. return macAddress;
  108. }
  109. }
  110. }