ComputerInfoTool.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //############################################################
  2. // https://github.com/yuzhengyang
  3. // author:yuzhengyang
  4. //############################################################
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Management;
  8. using System.Net.NetworkInformation;
  9. namespace Y.Utils.WindowsUtils.InfoUtils
  10. {
  11. public static class ComputerInfoTool
  12. {
  13. #region 获取CpuId
  14. public static string GetCpuId()
  15. {
  16. ManagementClass mc = null;
  17. ManagementObjectCollection moc = null;
  18. string ProcessorId = "";
  19. try
  20. {
  21. mc = new ManagementClass("Win32_Processor");
  22. moc = mc.GetInstances();
  23. foreach (ManagementObject mo in moc)
  24. {
  25. ProcessorId = mo.Properties["ProcessorId"].Value.ToString();
  26. }
  27. return ProcessorId;
  28. }
  29. catch
  30. {
  31. return "unknow";
  32. }
  33. finally
  34. {
  35. if (moc != null) moc.Dispose();
  36. if (mc != null) mc.Dispose();
  37. }
  38. }
  39. #endregion
  40. #region 获取CPU信息
  41. public static string GetCpuInfo()
  42. {
  43. try
  44. {
  45. string result = "";
  46. ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
  47. foreach (ManagementObject item in searcher.Get())
  48. {
  49. result = item["Name"].ToString();
  50. }
  51. return result;
  52. }
  53. catch
  54. { return "unknown"; }
  55. }
  56. #endregion
  57. #region 获取网卡信息
  58. /// <summary>
  59. /// 获取网卡信息
  60. /// Item1:描述,Item2:物理地址(Mac),Item3:Ip地址,Item4:网关地址
  61. /// </summary>
  62. /// <returns></returns>
  63. public static List<Tuple<string, string, string, string>> GetNetworkCardInfo()
  64. {
  65. try
  66. {
  67. List<Tuple<string, string, string, string>> result = new List<Tuple<string, string, string, string>>();
  68. NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
  69. foreach (var item in adapters)
  70. {
  71. if (item.NetworkInterfaceType == NetworkInterfaceType.Ethernet || item.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
  72. {
  73. string _desc = item.Description;
  74. string _mac = item.GetPhysicalAddress().ToString();
  75. string _ip = item.GetIPProperties().UnicastAddresses.Count >= 2 ?
  76. item.GetIPProperties().UnicastAddresses[1].Address.ToString() : null;
  77. string _gateway = item.GetIPProperties().GatewayAddresses.Count >= 1 ?
  78. item.GetIPProperties().GatewayAddresses[0].Address.ToString() : null;
  79. result.Add(new Tuple<string, string, string, string>(_desc, _mac, _ip, _gateway));
  80. }
  81. }
  82. return result;
  83. }
  84. catch (NetworkInformationException e)
  85. {
  86. return null;
  87. }
  88. }
  89. #endregion
  90. }
  91. }