使用C#获取CPU及硬盘序列号的源代码

简介:

首先需添加对System.Management的引用。

 
  1. view plaincopy to clipboardprint?  
  2. using System;      
  3. using System.Runtime.InteropServices;      
  4. using System.Management;      
  5.     
  6. namespace Hardware      
  7. {      
  8.     /// <summary>      
  9.     /// HardwareInfo 的摘要说明。      
  10.     /// </summary>      
  11.     public class HardwareInfo      
  12.     {      
  13.         //取机器名       
  14.         public string GetHostName()      
  15.         {      
  16.             return System.Net.Dns.GetHostName();      
  17.         }      
  18.         //取CPU编号      
  19.         public String GetCpuID()      
  20.         {      
  21.             try      
  22.             {      
  23.                 ManagementClass mc = new ManagementClass("Win32_Processor");      
  24.                 ManagementObjectCollection moc = mc.GetInstances();      
  25.     
  26.                 String strCpuID = null;      
  27.                 foreach (ManagementObject mo in moc)      
  28.                 {      
  29.                     strCpuID = mo.Properties["ProcessorId"].Value.ToString();      
  30.                     break;      
  31.                 }      
  32.                 return strCpuID;      
  33.             }      
  34.             catch      
  35.             {      
  36.                 return "";      
  37.             }      
  38.     
  39.         }//end method      
  40.     
  41.         //取第一块硬盘编号      
  42.         public String GetHardDiskID()      
  43.         {      
  44.             try      
  45.             {      
  46.                 ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");      
  47.                 String strHardDiskID = null;      
  48.                 foreach (ManagementObject mo in searcher.Get())      
  49.                 {      
  50.                     strHardDiskID = mo["SerialNumber"].ToString().Trim();      
  51.                     break;      
  52.                 }      
  53.                 return strHardDiskID;      
  54.             }      
  55.             catch      
  56.             {      
  57.                 return "";      
  58.             }      
  59.         }//end       
  60.     
  61.         public enum NCBCONST      
  62.         {      
  63.             NCBNAMSZ = 16,      /* absolute length of a net name         */      
  64.             MAX_LANA = 254,      /* lana's in range 0 to MAX_LANA inclusive   */      
  65.             NCBENUM = 0x37,      /* NCB ENUMERATE LANA NUMBERS            */      
  66.             NRC_GOODRET = 0x00,      /* good return                              */      
  67.             NCBRESET = 0x32,      /* NCB RESET                        */      
  68.             NCBASTAT = 0x33,      /* NCB ADAPTER STATUS                  */      
  69.             NUM_NAMEBUF = 30,      /* Number of NAME's BUFFER               */      
  70.         }      
  71.     
  72.         [StructLayout(LayoutKind.Sequential)]      
  73.         public struct ADAPTER_STATUS      
  74.         {      
  75.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]      
  76.             public byte[] adapter_address;      
  77.             public byte rev_major;      
  78.             public byte reserved0;      
  79.             public byte adapter_type;      
  80.             public byte rev_minor;      
  81.             public ushort duration;      
  82.             public ushort frmr_recv;      
  83.             public ushort frmr_xmit;      
  84.             public ushort iframe_recv_err;      
  85.             public ushort xmit_aborts;      
  86.             public uint xmit_success;      
  87.             public uint recv_success;      
  88.             public ushort iframe_xmit_err;      
  89.             public ushort recv_buff_unavail;      
  90.             public ushort t1_timeouts;      
  91.             public ushort ti_timeouts;      
  92.             public uint reserved1;      
  93.             public ushort free_ncbs;      
  94.             public ushort max_cfg_ncbs;      
  95.             public ushort max_ncbs;      
  96.             public ushort xmit_buf_unavail;      
  97.             public ushort max_dgram_size;      
  98.             public ushort pending_sess;      
  99.             public ushort max_cfg_sess;      
  100.             public ushort max_sess;      
  101.             public ushort max_sess_pkt_size;      
  102.             public ushort name_count;      
  103.         }      
  104.     
  105.         [StructLayout(LayoutKind.Sequential)]      
  106.         public struct NAME_BUFFER      
  107.         {      
  108.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]      
  109.             public byte[] name;      
  110.             public byte name_num;      
  111.             public byte name_flags;      
  112.         }      
  113.     
  114.         [StructLayout(LayoutKind.Sequential)]      
  115.         public struct NCB      
  116.         {      
  117.             public byte ncb_command;      
  118.             public byte ncb_retcode;      
  119.             public byte ncb_lsn;      
  120.             public byte ncb_num;      
  121.             public IntPtr ncb_buffer;      
  122.             public ushort ncb_length;      
  123.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]      
  124.             public byte[] ncb_callname;      
  125.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]      
  126.             public byte[] ncb_name;      
  127.             public byte ncb_rto;      
  128.             public byte ncb_sto;      
  129.             public IntPtr ncb_post;      
  130.             public byte ncb_lana_num;      
  131.             public byte ncb_cmd_cplt;      
  132.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]      
  133.             public byte[] ncb_reserve;      
  134.             public IntPtr ncb_event;      
  135.         }      
  136.     
  137.         [StructLayout(LayoutKind.Sequential)]      
  138.         public struct LANA_ENUM      
  139.         {      
  140.             public byte length;      
  141.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.MAX_LANA)]      
  142.             public byte[] lana;      
  143.         }      
  144.     
  145.         [StructLayout(LayoutKind.Auto)]      
  146.         public struct ASTAT      
  147.         {      
  148.             public ADAPTER_STATUS adapt;      
  149.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NUM_NAMEBUF)]      
  150.             public NAME_BUFFER[] NameBuff;      
  151.         }      
  152.         public class Win32API      
  153.         {      
  154.             [DllImport("NETAPI32.DLL")]      
  155.             public static extern char Netbios(ref NCB ncb);      
  156.         }      
  157.     
  158.         public string GetMacAddress()      
  159.         {      
  160.             string addr = "";      
  161.             try      
  162.             {      
  163.                 int cb;      
  164.                 ASTAT adapter;      
  165.                 NCB Ncb = new NCB();      
  166.                 char uRetCode;      
  167.                 LANA_ENUM lenum;      
  168.     
  169.                 Ncb.ncb_command = (byte)NCBCONST.NCBENUM;      
  170.                 cb = Marshal.SizeOf(typeof(LANA_ENUM));      
  171.                 Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);      
  172.                 Ncb.ncb_length = (ushort)cb;      
  173.                 uRetCode = Win32API.Netbios(ref Ncb);      
  174.                 lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM));      
  175.                 Marshal.FreeHGlobal(Ncb.ncb_buffer);      
  176.                 if (uRetCode != (short)NCBCONST.NRC_GOODRET)      
  177.                     return "";      
  178.     
  179.                 for (int i = 0; i < lenum.length; i++)      
  180.                 {      
  181.                     Ncb.ncb_command = (byte)NCBCONST.NCBRESET;      
  182.                     Ncb.ncb_lana_num = lenum.lana[i];      
  183.                     uRetCode = Win32API.Netbios(ref Ncb);      
  184.                     if (uRetCode != (short)NCBCONST.NRC_GOODRET)      
  185.                         return "";      
  186.     
  187.                     Ncb.ncb_command = (byte)NCBCONST.NCBASTAT;      
  188.                     Ncb.ncb_lana_num = lenum.lana[i];      
  189.                     Ncb.ncb_callname[0] = (byte)'*';      
  190.                     cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER)) * (int)NCBCONST.NUM_NAMEBUF;      
  191.                     Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);      
  192.                     Ncb.ncb_length = (ushort)cb;      
  193.                     uRetCode = Win32API.Netbios(ref Ncb);      
  194.                     adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS));      
  195.                     Marshal.FreeHGlobal(Ncb.ncb_buffer);      
  196.     
  197.                     if (uRetCode == (short)NCBCONST.NRC_GOODRET)      
  198.                     {      
  199.                         if (i > 0)      
  200.                             addr += ":";      
  201.                         addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}",      
  202.                          adapter.adapt.adapter_address[0],      
  203.                          adapter.adapt.adapter_address[1],      
  204.                          adapter.adapt.adapter_address[2],      
  205.                          adapter.adapt.adapter_address[3],      
  206.                          adapter.adapt.adapter_address[4],      
  207.                          adapter.adapt.adapter_address[5]);      
  208.                     }      
  209.                 }      
  210.             }      
  211.             catch      
  212.             { }      
  213.             return addr.Replace(' ''0');      
  214.         }      
  215.     }      
  216. }     
  217. using System;   
  218. using System.Runtime.InteropServices;   
  219. using System.Management;   
  220.  
  221. namespace Hardware   
  222. {   
  223.     /// <summary>   
  224.     /// HardwareInfo 的摘要说明。   
  225.     /// </summary>   
  226.     public class HardwareInfo   
  227.     {   
  228.         //取机器名    
  229.         public string GetHostName()   
  230.         {   
  231.             return System.Net.Dns.GetHostName();   
  232.         }   
  233.         //取CPU编号   
  234.         public String GetCpuID()   
  235.         {   
  236.             try   
  237.             {   
  238.                 ManagementClass mc = new ManagementClass("Win32_Processor");   
  239.                 ManagementObjectCollection moc = mc.GetInstances();   
  240.  
  241.                 String strCpuID = null;   
  242.                 foreach (ManagementObject mo in moc)   
  243.                 {   
  244.                     strCpuID = mo.Properties["ProcessorId"].Value.ToString();   
  245.                     break;   
  246.                 }   
  247.                 return strCpuID;   
  248.             }   
  249.             catch   
  250.             {   
  251.                 return "";   
  252.             }   
  253.  
  254.         }//end method   
  255.  
  256.         //取第一块硬盘编号   
  257.         public String GetHardDiskID()   
  258.         {   
  259.             try   
  260.             {   
  261.                 ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");   
  262.                 String strHardDiskID = null;   
  263.                 foreach (ManagementObject mo in searcher.Get())   
  264.                 {   
  265.                     strHardDiskID = mo["SerialNumber"].ToString().Trim();   
  266.                     break;   
  267.                 }   
  268.                 return strHardDiskID;   
  269.             }   
  270.             catch   
  271.             {   
  272.                 return "";   
  273.             }   
  274.         }//end    
  275.  
  276.         public enum NCBCONST   
  277.         {   
  278.             NCBNAMSZ = 16,      /* absolute length of a net name         */   
  279.             MAX_LANA = 254,      /* lana's in range 0 to MAX_LANA inclusive   */   
  280.             NCBENUM = 0x37,      /* NCB ENUMERATE LANA NUMBERS            */   
  281.             NRC_GOODRET = 0x00,      /* good return                              */   
  282.             NCBRESET = 0x32,      /* NCB RESET                        */   
  283.             NCBASTAT = 0x33,      /* NCB ADAPTER STATUS                  */   
  284.             NUM_NAMEBUF = 30,      /* Number of NAME's BUFFER               */   
  285.         }   
  286.  
  287.         [StructLayout(LayoutKind.Sequential)]   
  288.         public struct ADAPTER_STATUS   
  289.         {   
  290.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]   
  291.             public byte[] adapter_address;   
  292.             public byte rev_major;   
  293.             public byte reserved0;   
  294.             public byte adapter_type;   
  295.             public byte rev_minor;   
  296.             public ushort duration;   
  297.             public ushort frmr_recv;   
  298.             public ushort frmr_xmit;   
  299.             public ushort iframe_recv_err;   
  300.             public ushort xmit_aborts;   
  301.             public uint xmit_success;   
  302.             public uint recv_success;   
  303.             public ushort iframe_xmit_err;   
  304.             public ushort recv_buff_unavail;   
  305.             public ushort t1_timeouts;   
  306.             public ushort ti_timeouts;   
  307.             public uint reserved1;   
  308.             public ushort free_ncbs;   
  309.             public ushort max_cfg_ncbs;   
  310.             public ushort max_ncbs;   
  311.             public ushort xmit_buf_unavail;   
  312.             public ushort max_dgram_size;   
  313.             public ushort pending_sess;   
  314.             public ushort max_cfg_sess;   
  315.             public ushort max_sess;   
  316.             public ushort max_sess_pkt_size;   
  317.             public ushort name_count;   
  318.         }   
  319.  
  320.         [StructLayout(LayoutKind.Sequential)]   
  321.         public struct NAME_BUFFER   
  322.         {   
  323.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]   
  324.             public byte[] name;   
  325.             public byte name_num;   
  326.             public byte name_flags;   
  327.         }   
  328.  
  329.         [StructLayout(LayoutKind.Sequential)]   
  330.         public struct NCB   
  331.         {   
  332.             public byte ncb_command;   
  333.             public byte ncb_retcode;   
  334.             public byte ncb_lsn;   
  335.             public byte ncb_num;   
  336.             public IntPtr ncb_buffer;   
  337.             public ushort ncb_length;   
  338.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]   
  339.             public byte[] ncb_callname;   
  340.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]   
  341.             public byte[] ncb_name;   
  342.             public byte ncb_rto;   
  343.             public byte ncb_sto;   
  344.             public IntPtr ncb_post;   
  345.             public byte ncb_lana_num;   
  346.             public byte ncb_cmd_cplt;   
  347.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]   
  348.             public byte[] ncb_reserve;   
  349.             public IntPtr ncb_event;   
  350.         }   
  351.  
  352.         [StructLayout(LayoutKind.Sequential)]   
  353.         public struct LANA_ENUM   
  354.         {   
  355.             public byte length;   
  356.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.MAX_LANA)]   
  357.             public byte[] lana;   
  358.         }   
  359.  
  360.         [StructLayout(LayoutKind.Auto)]   
  361.         public struct ASTAT   
  362.         {   
  363.             public ADAPTER_STATUS adapt;   
  364.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NUM_NAMEBUF)]   
  365.             public NAME_BUFFER[] NameBuff;   
  366.         }   
  367.         public class Win32API   
  368.         {   
  369.             [DllImport("NETAPI32.DLL")]   
  370.             public static extern char Netbios(ref NCB ncb);   
  371.         }   
  372.  
  373.         public string GetMacAddress()   
  374.         {   
  375.             string addr = "";   
  376.             try   
  377.             {   
  378.                 int cb;   
  379.                 ASTAT adapter;   
  380.                 NCB Ncb = new NCB();   
  381.                 char uRetCode;   
  382.                 LANA_ENUM lenum;   
  383.  
  384.                 Ncb.ncb_command = (byte)NCBCONST.NCBENUM;   
  385.                 cb = Marshal.SizeOf(typeof(LANA_ENUM));   
  386.                 Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);   
  387.                 Ncb.ncb_length = (ushort)cb;   
  388.                 uRetCode = Win32API.Netbios(ref Ncb);   
  389.                 lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM));   
  390.                 Marshal.FreeHGlobal(Ncb.ncb_buffer);   
  391.                 if (uRetCode != (short)NCBCONST.NRC_GOODRET)   
  392.                     return "";   
  393.  
  394.                 for (int i = 0; i < lenum.length; i++)   
  395.                 {   
  396.                     Ncb.ncb_command = (byte)NCBCONST.NCBRESET;   
  397.                     Ncb.ncb_lana_num = lenum.lana[i];   
  398.                     uRetCode = Win32API.Netbios(ref Ncb);   
  399.                     if (uRetCode != (short)NCBCONST.NRC_GOODRET)   
  400.                         return "";   
  401.  
  402.                     Ncb.ncb_command = (byte)NCBCONST.NCBASTAT;   
  403.                     Ncb.ncb_lana_num = lenum.lana[i];   
  404.                     Ncb.ncb_callname[0] = (byte)'*';   
  405.                     cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER)) * (int)NCBCONST.NUM_NAMEBUF;   
  406.                     Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);   
  407.                     Ncb.ncb_length = (ushort)cb;   
  408.                     uRetCode = Win32API.Netbios(ref Ncb);   
  409.                     adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS));   
  410.                     Marshal.FreeHGlobal(Ncb.ncb_buffer);   
  411.  
  412.                     if (uRetCode == (short)NCBCONST.NRC_GOODRET)   
  413.                     {   
  414.                         if (i > 0)   
  415.                             addr += ":";   
  416.                         addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}",   
  417.                          adapter.adapt.adapter_address[0],   
  418.                          adapter.adapt.adapter_address[1],   
  419.                          adapter.adapt.adapter_address[2],   
  420.                          adapter.adapt.adapter_address[3],   
  421.                          adapter.adapt.adapter_address[4],   
  422.                          adapter.adapt.adapter_address[5]);   
  423.                     }   
  424.                 }   
  425.             }   
  426.             catch   
  427.             { }   
  428.             return addr.Replace(' ''0');   
  429.         }   
  430.     }   
  431. }   
  432.    
  433.  

使用方法举例:

 
  1. view plaincopy to clipboardprint?  
  2. //获取硬盘序列号      
  3. Hardware.HardwareInfo hardwareInfo = new Hardware.HardwareInfo();      
  4. string hardDiskID = hardwareInfo.GetHardDiskID();      
  5. System.Console.WriteLine(hardDiskID);      
  6.     
  7. //获取CPU序列号      
  8. string cpuID = hardwareInfo.GetCpuID();      
  9. System.Console.WriteLine(cpuID);      
  10. //获取硬盘序列号Hardware.HardwareInfo hardwareInfo = new Hardware.HardwareInfo();string hardDiskID = hardwareInfo.GetHardDiskID();System.Console.WriteLine(hardDiskID);//获取CPU序列号string cpuID = hardwareInfo.GetCpuID();System.Console.WriteLine(cpuID);    

 






 本文转自 破狼 51CTO博客,原文链接:http://blog.51cto.com/whitewolfblog/833603,如需转载请自行联系原作者


相关文章
|
4月前
|
XML Ubuntu Linux
部署08---扩展-Win10配置WSL(Ubuntu)环境,WSL系统是什么意思,是Windows系统上的一个子系统, xml的一大特点是直链系统,直接链接你的CPU,硬盘和内存,如何用 WSL部署
部署08---扩展-Win10配置WSL(Ubuntu)环境,WSL系统是什么意思,是Windows系统上的一个子系统, xml的一大特点是直链系统,直接链接你的CPU,硬盘和内存,如何用 WSL部署
|
1月前
|
C# 开发工具 Windows
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
40 0
|
3月前
|
设计模式 uml
在电脑主机(MainFrame)中只需要按下主机的开机按钮(on()),即可调用其它硬件设备和软件的启动方法,如内存(Memory)的自检(check())、CPU的运行(run())、硬盘(Hard
该博客文章通过一个电脑主机启动的示例代码,展示了外观模式(Facade Pattern)的设计模式,其中主机(MainFrame)类通过调用内部硬件组件(如内存、CPU、硬盘)和操作系统的启动方法来实现开机流程,同时讨论了外观模式的优缺点。
|
3月前
|
编译器 C# Windows
C#基础:手动编译一个.cs源代码文件并生成.exe可执行文件
通过上述步骤,应该能够高效准确地编译C#源代码并生成相应的可执行文件。此外,这一过程强调了对命令行编译器的理解,这在调试和自动化编译流程中是非常重要的。
236 2
|
3月前
|
KVM 虚拟化
[kvm]cpu内存硬盘配置
[kvm]cpu内存硬盘配置
|
4月前
|
Linux 调度
部署02-我们一般接触的是Mos和Wimdows这两款操作系统,很少接触到Linux,操作系统的概述,硬件是由计算机系统中由电子和机械,光电元件所组成的,CPU,内存,硬盘,软件是用户与计算机接口之间
部署02-我们一般接触的是Mos和Wimdows这两款操作系统,很少接触到Linux,操作系统的概述,硬件是由计算机系统中由电子和机械,光电元件所组成的,CPU,内存,硬盘,软件是用户与计算机接口之间
|
5月前
|
人工智能 缓存 Java
技术经验解读:【转】详细解析用C#写的小游戏《彩色连珠》(附源代码)
技术经验解读:【转】详细解析用C#写的小游戏《彩色连珠》(附源代码)
25 0
|
6月前
|
监控 Shell
Shell脚本监控CPU、内存和硬盘利用率
Shell脚本监控CPU、内存和硬盘利用率
|
6月前
|
运维 Linux
Linux 查询 OS、CPU、内存、硬盘信息
Linux 查询 OS、CPU、内存、硬盘信息
137 0
|
监控 Shell Perl
监控CPU、内存和硬盘利用率
监控CPU、内存和硬盘利用率
145 1