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

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

view plaincopy to clipboardprint?
using System;    
using System.Runtime.InteropServices;    
using System.Management;    
  
namespace Hardware    
{    
    /// <summary>    
    /// HardwareInfo 的摘要说明。    
    /// </summary>    
    public class HardwareInfo    
    {    
        //取机器名     
        public string GetHostName()    
        {    
            return System.Net.Dns.GetHostName();    
        }    
        //取CPU编号    
        public String GetCpuID()    
        {    
            try    
            {    
                ManagementClass mc = new ManagementClass("Win32_Processor");    
                ManagementObjectCollection moc = mc.GetInstances();    
  
                String strCpuID = null;    
                foreach (ManagementObject mo in moc)    
                {    
                    strCpuID = mo.Properties["ProcessorId"].Value.ToString();    
                    break;    
                }    
                return strCpuID;    
            }    
            catch    
            {    
                return "";    
            }    
  
        }//end method    
  
        //取第一块硬盘编号    
        public String GetHardDiskID()    
        {    
            try    
            {    
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");    
                String strHardDiskID = null;    
                foreach (ManagementObject mo in searcher.Get())    
                {    
                    strHardDiskID = mo["SerialNumber"].ToString().Trim();    
                    break;    
                }    
                return strHardDiskID;    
            }    
            catch    
            {    
                return "";    
            }    
        }//end     
  
        public enum NCBCONST    
        {    
            NCBNAMSZ = 16,      /* absolute length of a net name         */    
            MAX_LANA = 254,      /* lana's in range 0 to MAX_LANA inclusive   */    
            NCBENUM = 0x37,      /* NCB ENUMERATE LANA NUMBERS            */    
            NRC_GOODRET = 0x00,      /* good return                              */    
            NCBRESET = 0x32,      /* NCB RESET                        */    
            NCBASTAT = 0x33,      /* NCB ADAPTER STATUS                  */    
            NUM_NAMEBUF = 30,      /* Number of NAME's BUFFER               */    
        }    
  
        [StructLayout(LayoutKind.Sequential)]    
        public struct ADAPTER_STATUS    
        {    
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]    
            public byte[] adapter_address;    
            public byte rev_major;    
            public byte reserved0;    
            public byte adapter_type;    
            public byte rev_minor;    
            public ushort duration;    
            public ushort frmr_recv;    
            public ushort frmr_xmit;    
            public ushort iframe_recv_err;    
            public ushort xmit_aborts;    
            public uint xmit_success;    
            public uint recv_success;    
            public ushort iframe_xmit_err;    
            public ushort recv_buff_unavail;    
            public ushort t1_timeouts;    
            public ushort ti_timeouts;    
            public uint reserved1;    
            public ushort free_ncbs;    
            public ushort max_cfg_ncbs;    
            public ushort max_ncbs;    
            public ushort xmit_buf_unavail;    
            public ushort max_dgram_size;    
            public ushort pending_sess;    
            public ushort max_cfg_sess;    
            public ushort max_sess;    
            public ushort max_sess_pkt_size;    
            public ushort name_count;    
        }    
  
        [StructLayout(LayoutKind.Sequential)]    
        public struct NAME_BUFFER    
        {    
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]    
            public byte[] name;    
            public byte name_num;    
            public byte name_flags;    
        }    
  
        [StructLayout(LayoutKind.Sequential)]    
        public struct NCB    
        {    
            public byte ncb_command;    
            public byte ncb_retcode;    
            public byte ncb_lsn;    
            public byte ncb_num;    
            public IntPtr ncb_buffer;    
            public ushort ncb_length;    
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]    
            public byte[] ncb_callname;    
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)]    
            public byte[] ncb_name;    
            public byte ncb_rto;    
            public byte ncb_sto;    
            public IntPtr ncb_post;    
            public byte ncb_lana_num;    
            public byte ncb_cmd_cplt;    
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]    
            public byte[] ncb_reserve;    
            public IntPtr ncb_event;    
        }    
  
        [StructLayout(LayoutKind.Sequential)]    
        public struct LANA_ENUM    
        {    
            public byte length;    
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.MAX_LANA)]    
            public byte[] lana;    
        }    
  
        [StructLayout(LayoutKind.Auto)]    
        public struct ASTAT    
        {    
            public ADAPTER_STATUS adapt;    
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NUM_NAMEBUF)]    
            public NAME_BUFFER[] NameBuff;    
        }    
        public class Win32API    
        {    
            [DllImport("NETAPI32.DLL")]    
            public static extern char Netbios(ref NCB ncb);    
        }    
  
        public string GetMacAddress()    
        {    
            string addr = "";    
            try    
            {    
                int cb;    
                ASTAT adapter;    
                NCB Ncb = new NCB();    
                char uRetCode;    
                LANA_ENUM lenum;    
  
                Ncb.ncb_command = (byte)NCBCONST.NCBENUM;    
                cb = Marshal.SizeOf(typeof(LANA_ENUM));    
                Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);    
                Ncb.ncb_length = (ushort)cb;    
                uRetCode = Win32API.Netbios(ref Ncb);    
                lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM));    
                Marshal.FreeHGlobal(Ncb.ncb_buffer);    
                if (uRetCode != (short)NCBCONST.NRC_GOODRET)    
                    return "";    
  
                for (int i = 0; i < lenum.length; i++)    
                {    
                    Ncb.ncb_command = (byte)NCBCONST.NCBRESET;    
                    Ncb.ncb_lana_num = lenum.lana[i];    
                    uRetCode = Win32API.Netbios(ref Ncb);    
                    if (uRetCode != (short)NCBCONST.NRC_GOODRET)    
                        return "";    
  
                    Ncb.ncb_command = (byte)NCBCONST.NCBASTAT;    
                    Ncb.ncb_lana_num = lenum.lana[i];    
                    Ncb.ncb_callname[0] = (byte)'*';    
                    cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER)) * (int)NCBCONST.NUM_NAMEBUF;    
                    Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);    
                    Ncb.ncb_length = (ushort)cb;    
                    uRetCode = Win32API.Netbios(ref Ncb);    
                    adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS));   
                    Marshal.FreeHGlobal(Ncb.ncb_buffer);    
  
                    if (uRetCode == (short)NCBCONST.NRC_GOODRET)    
                    {    
                        if (i > 0)    
                            addr += ":";    
                        addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}",    
                         adapter.adapt.adapter_address[0],    
                         adapter.adapt.adapter_address[1],    
                         adapter.adapt.adapter_address[2],    
                         adapter.adapt.adapter_address[3],    
                         adapter.adapt.adapter_address[4],    
                         adapter.adapt.adapter_address[5]);    
                    }    
                }    
            }    
            catch    
            { }    
            return addr.Replace(' ', '0');    
        }    
    }    
}   
using System; 
using System.Runtime.InteropServices; 
using System.Management;

namespace Hardware 

    /// <summary> 
    /// HardwareInfo 的摘要说明。 
    /// </summary> 
    public class HardwareInfo 
    { 
        //取机器名  
        public string GetHostName() 
        { 
            return System.Net.Dns.GetHostName(); 
        } 
        //取CPU编号 
        public String GetCpuID() 
        { 
            try 
            { 
                ManagementClass mc = new ManagementClass("Win32_Processor"); 
                ManagementObjectCollection moc = mc.GetInstances();

                String strCpuID = null; 
                foreach (ManagementObject mo in moc) 
                { 
                    strCpuID = mo.Properties["ProcessorId"].Value.ToString(); 
                    break; 
                } 
                return strCpuID; 
            } 
            catch 
            { 
                return ""; 
            }

        }//end method

        //取第一块硬盘编号 
        public String GetHardDiskID() 
        { 
            try 
            { 
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia"); 
                String strHardDiskID = null; 
                foreach (ManagementObject mo in searcher.Get()) 
                { 
                    strHardDiskID = mo["SerialNumber"].ToString().Trim(); 
                    break; 
                } 
                return strHardDiskID; 
            } 
            catch 
            { 
                return ""; 
            } 
        }//end 

        public enum NCBCONST 
        { 
            NCBNAMSZ = 16,      /* absolute length of a net name         */ 
            MAX_LANA = 254,      /* lana's in range 0 to MAX_LANA inclusive   */ 
            NCBENUM = 0x37,      /* NCB ENUMERATE LANA NUMBERS            */ 
            NRC_GOODRET = 0x00,      /* good return                              */ 
            NCBRESET = 0x32,      /* NCB RESET                        */ 
            NCBASTAT = 0x33,      /* NCB ADAPTER STATUS                  */ 
            NUM_NAMEBUF = 30,      /* Number of NAME's BUFFER               */ 
        }

        [StructLayout(LayoutKind.Sequential)] 
        public struct ADAPTER_STATUS 
        { 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] 
            public byte[] adapter_address; 
            public byte rev_major; 
            public byte reserved0; 
            public byte adapter_type; 
            public byte rev_minor; 
            public ushort duration; 
            public ushort frmr_recv; 
            public ushort frmr_xmit; 
            public ushort iframe_recv_err; 
            public ushort xmit_aborts; 
            public uint xmit_success; 
            public uint recv_success; 
            public ushort iframe_xmit_err; 
            public ushort recv_buff_unavail; 
            public ushort t1_timeouts; 
            public ushort ti_timeouts; 
            public uint reserved1; 
            public ushort free_ncbs; 
            public ushort max_cfg_ncbs; 
            public ushort max_ncbs; 
            public ushort xmit_buf_unavail; 
            public ushort max_dgram_size; 
            public ushort pending_sess; 
            public ushort max_cfg_sess; 
            public ushort max_sess; 
            public ushort max_sess_pkt_size; 
            public ushort name_count; 
        }

        [StructLayout(LayoutKind.Sequential)] 
        public struct NAME_BUFFER 
        { 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)] 
            public byte[] name; 
            public byte name_num; 
            public byte name_flags; 
        }

        [StructLayout(LayoutKind.Sequential)] 
        public struct NCB 
        { 
            public byte ncb_command; 
            public byte ncb_retcode; 
            public byte ncb_lsn; 
            public byte ncb_num; 
            public IntPtr ncb_buffer; 
            public ushort ncb_length; 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)] 
            public byte[] ncb_callname; 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NCBNAMSZ)] 
            public byte[] ncb_name; 
            public byte ncb_rto; 
            public byte ncb_sto; 
            public IntPtr ncb_post; 
            public byte ncb_lana_num; 
            public byte ncb_cmd_cplt; 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] 
            public byte[] ncb_reserve; 
            public IntPtr ncb_event; 
        }

        [StructLayout(LayoutKind.Sequential)] 
        public struct LANA_ENUM 
        { 
            public byte length; 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.MAX_LANA)] 
            public byte[] lana; 
        }

        [StructLayout(LayoutKind.Auto)] 
        public struct ASTAT 
        { 
            public ADAPTER_STATUS adapt; 
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = (int)NCBCONST.NUM_NAMEBUF)] 
            public NAME_BUFFER[] NameBuff; 
        } 
        public class Win32API 
        { 
            [DllImport("NETAPI32.DLL")] 
            public static extern char Netbios(ref NCB ncb); 
        }

        public string GetMacAddress() 
        { 
            string addr = ""; 
            try 
            { 
                int cb; 
                ASTAT adapter; 
                NCB Ncb = new NCB(); 
                char uRetCode; 
                LANA_ENUM lenum;

                Ncb.ncb_command = (byte)NCBCONST.NCBENUM; 
                cb = Marshal.SizeOf(typeof(LANA_ENUM)); 
                Ncb.ncb_buffer = Marshal.AllocHGlobal(cb); 
                Ncb.ncb_length = (ushort)cb; 
                uRetCode = Win32API.Netbios(ref Ncb); 
                lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM)); 
                Marshal.FreeHGlobal(Ncb.ncb_buffer); 
                if (uRetCode != (short)NCBCONST.NRC_GOODRET) 
                    return "";

                for (int i = 0; i < lenum.length; i++) 
                { 
                    Ncb.ncb_command = (byte)NCBCONST.NCBRESET; 
                    Ncb.ncb_lana_num = lenum.lana[i]; 
                    uRetCode = Win32API.Netbios(ref Ncb); 
                    if (uRetCode != (short)NCBCONST.NRC_GOODRET) 
                        return "";

                    Ncb.ncb_command = (byte)NCBCONST.NCBASTAT; 
                    Ncb.ncb_lana_num = lenum.lana[i]; 
                    Ncb.ncb_callname[0] = (byte)'*'; 
                    cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER)) * (int)NCBCONST.NUM_NAMEBUF; 
                    Ncb.ncb_buffer = Marshal.AllocHGlobal(cb); 
                    Ncb.ncb_length = (ushort)cb; 
                    uRetCode = Win32API.Netbios(ref Ncb); 
                    adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS)); 
                    Marshal.FreeHGlobal(Ncb.ncb_buffer);

                    if (uRetCode == (short)NCBCONST.NRC_GOODRET) 
                    { 
                        if (i > 0) 
                            addr += ":"; 
                        addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}", 
                         adapter.adapt.adapter_address[0], 
                         adapter.adapt.adapter_address[1], 
                         adapter.adapt.adapter_address[2], 
                         adapter.adapt.adapter_address[3], 
                         adapter.adapt.adapter_address[4], 
                         adapter.adapt.adapter_address[5]); 
                    } 
                } 
            } 
            catch 
            { } 
            return addr.Replace(' ', '0'); 
        } 
    } 

 

使用方法举例:

view plaincopy to clipboardprint?
//获取硬盘序列号    
Hardware.HardwareInfo hardwareInfo = new Hardware.HardwareInfo();    
string hardDiskID = hardwareInfo.GetHardDiskID();    
System.Console.WriteLine(hardDiskID);    
  
//获取CPU序列号    
string cpuID = hardwareInfo.GetCpuID();    
System.Console.WriteLine(cpuID);    
//获取硬盘序列号Hardware.HardwareInfo hardwareInfo = new Hardware.HardwareInfo();string hardDiskID = hardwareInfo.GetHardDiskID();System.Console.WriteLine(hardDiskID);//获取CPU序列号string cpuID = hardwareInfo.GetCpuID();System.Console.WriteLine(cpuID); 

本文转载自CSDN博客,http://blog.csdn.net/songkexin/archive/2009/12/01/4916602.aspx

作者:破  狼 
出处:http://www.cnblogs.com/whitewolf/ 
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客博客园--破狼51CTO--破狼。http://www.cnblogs.com/whitewolf/archive/2009/12/11/1622256.html

时间: 2024-11-05 14:31:40

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

我使用WEB,如何在程序中,获取CPU、硬盘、IP地址及其它信息?

问题描述 我使用WEB,如何在程序中,获取CPU.硬盘.IP地址及其它信息? 解决方案 解决方案二:获取Server上的信息?先找到本地命令,能看到这些信息的,然后在程序中用Runtime.exec(去执行这些本地命令.解决方案三:估计他想获得用户的这些信息!如果是这样的话,想都不用想了!解决方案四:IP地址能获取当客户端的其它信息通过java是没办法的不晓得别的技术能行不webstart方式运行的客户端还行web方式的话active插件不晓得可以不(纯粹猜测,没用过)解决方案五:用户的这些信息

VB.net获取CPU与硬盘物理序列号与容量的实例

首先,引用System.Management:然后在代码中Imports System.Management: 以下功能主要依靠wmi实现 获得硬盘序列号  代码如下 复制代码 Dim cmicWmi As New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive") Dim Uint32 As UInt32 For Each cmicWmiObj As ManagementObject

C#获取本机的MAC地址\序列号\硬盘序列号

1 /// <summary> 2 /// 显示MAC地址 3 /// </summary> 4 /// <returns></returns> 5 private string GetMAC() 6 { 7 string mac = "本机的MAC地址:"; 8 using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration&

&lt;转&gt;另一个获取硬盘序列号的方法

原谅转自:http://blog.sina.com.cn/s/blog_57dff12f0100d5so.html   #define  DFP_GET_VERSION                    0x00074080#define  FILE_DEVICE_SCSI                   0x0000001b#define  IOCTL_SCSI_MINIPORT_IDENTIFY       ((FILE_DEVICE_SCSI << 16 ) + 0x0501)#

C#应用:获取cpu序列号,硬盘ID,网卡MAC地址

硬盘 private void GetInfo()  {   string cpuInfo = "";//cpu序列号   ManagementClass cimobject = new ManagementClass("Win32_Processor");   ManagementObjectCollection moc = cimobject.GetInstances();   foreach(ManagementObject mo in moc)   {   

VC++获取网卡MAC、硬盘序列号、CPU ID、BIOS编号

  以下代码可以取得系统特征码(网卡MAC.硬盘序列号.CPU ID.BIOS编号)     BYTE szSystemInfo[4096]; // 在程序执行完毕后,此处存储取得的系统特征码     UINT uSystemInfoLen = 0; // 在程序执行完毕后,此处存储取得的系统特征码的长度     // 网卡 MAC 地址,注意: MAC 地址是可以在注册表中修改的     {         UINT uErrorCode=0;         IP_ADAPTER_INFO

java如何获取网卡MAC、硬盘序列号、CPU ID

问题描述 java如何获取网卡MAC.硬盘序列号.CPUID 解决方案 解决方案二:c:以下代码可以取得系统特征码(网卡MAC.硬盘序列号.CPUID.BIOS编号)BYTEszSystemInfo[4096];//在程序执行完毕后,此处存储取得的系统特征码UINTuSystemInfoLen=0;//在程序执行完毕后,此处存储取得的系统特征码的长度//网卡MAC地址,注意:MAC地址是可以在注册表中修改的{UINTuErrorCode=0;IP_ADAPTER_INFOiai;ULONGuSi

如何获取电脑硬盘序列号?

问题描述 三个问题:1.如何获取电脑硬盘序列号?2.如何获取电脑CPU序列号?3.如何获取电脑主板序列号?三个问题基本类似,希望给出可用的代码,要C#的,其他语言不太熟悉,最好别给.望各位高手不吝赐教,谢谢!更多0 解决方案 解决方案二:这种问题应该找度娘的解决方案三:获取硬盘的序列号:http://jingyan.baidu.com/article/215817f7e5f1b71eda1423f1.html获取CPU序列号:http://jingyan.baidu.com/article/91

ubuntu如何获取CPU序列号或者主板序列号?

ubuntu如何获取CPU序列号或者主板序列号? CPU ID 代码: sudo dmidecode -t 4 | grep ID Serial Number 代码: sudo dmidecode -t 2 | grep Serial 通过以下两条命令的操作即可查出相关的序列号啦!