您现在的位置是: 首页 >  .NET开发 >  文章详情 文章详情

C#获取当前主机硬件信息

2019-08-19 【.NET开发】 1377人浏览

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using System.Threading.Tasks;

  6. using System.Net;

  7. using System.Management;  //在项目-》添加引用....里面引用System.Management

  8. using System.Runtime.InteropServices;

  9. namespace FileSplit

  10. {

  11. //此类用于获取当前主机的相关信息

  12. public class MachineInfo

  13. {

  14. //用法示例

  15. private string example()

  16. {

  17. string Info = "";

  18. MachineInfo info = MachineInfo.I();       //获取主机的对象信息

  19. Info = "\r\n主机名:" + Dns.GetHostName(); //获取主机名称

  20. Info += "\r\n系统类型:" + info.GetSystemType();

  21. Info += "\r\n物理内存:" + info.GetPhysicalMemory();

  22. Info += "\r\n\r\n本地IP:";

  23. string[] LocalIp = info.GetLocalIpAddress();

  24. foreach (string ip in LocalIp) Info += "\r\n" + ip;

  25. Info += "\r\n\r\n外网IP:";

  26. string[] ExternalIp = info.GetExtenalIpAddress();

  27. foreach (string ip in ExternalIp) Info += "\r\n" + ip;

  28. Info += "\r\n\r\nMAC:";

  29. Info += "\r\n" + info.GetNetCardMACAddress();

  30. //Info += "\r\n" + info.GetMacAddress(LocalIp[0]);

  31. Info += "\r\n";

  32. Info += "\r\nCPU序列号:" + info.GetCPUSerialNumber();

  33. //Info += "\r\nCPU编号:" + info.GetCPUID();

  34. //Info += "\r\nCPU版本信息:" + info.GetCPUVersion();

  35. //Info += "\r\nCPU名称信息:" + info.GetCPUName();

  36. //Info += "\r\nCPU制造厂商:" + info.GetCPUManufacturer();

  37. //Info += "\r\n主板制造厂商:" + info.GetBoardManufacturer();

  38. //Info += "\r\n主板序列号:" + info.GetBIOSSerialNumber();

  39. //Info += "\r\n硬盘序列号:" + info.GetHardDiskSerialNumber();

  40. //Info += "\r\n显卡PNPDeviceID:\r\n" + info.GetVideoPNPID();

  41. //Info += "\r\n声卡PNPDeviceID:\r\n" + info.GetSoundPNPID();

  42. return Info;

  43. }

  44. static MachineInfo Instance;

  45. ///

  46. /// 获取当前类对象的一个实例

  47. ///

  48. public static MachineInfo I()

  49. {

  50. if (Instance == null) Instance = new MachineInfo();

  51. return Instance;

  52. }

  53. ///

  54. /// 获取本地ip地址,多个ip

  55. ///

  56. public String[] GetLocalIpAddress()

  57. {

  58. string hostName = Dns.GetHostName();                    //获取主机名称

  59. IPAddress[] addresses = Dns.GetHostAddresses(hostName); //解析主机IP地址

  60. string[] IP = new string[addresses.Length];             //转换为字符串形式

  61. for (int i = 0; i