概述
最近学习过程中,想到提取系统硬件信息做一些验证,故而对网上提到的利用.NET System.Management类获取硬件信息做了进一步的学习、验证。验证是分别在4台电脑,XP SP3系统中进行,特将验证过程记录于此。
说明:
电脑1(联想品牌电脑);
电脑2(HP品牌电脑);
电脑3(联想品牌电脑);
电脑4(兼容机);
- 获取CPU编号:
返回结果:
电脑1:CPU ID:BFEBFBFF00000F27
电脑2:CPU ID:BFEBFBFF00000F27
电脑3:CPU ID:BFEBFBFF00000F29
电脑4:CPU ID:BFEBFBFF00000F29
- 获取主板编号:
返回结果:
电脑1:主板 ID:
电脑2:主板 ID:CN24401483
电脑3:主板 ID:AZF241001101
电脑4:主板 ID:
- 获取硬盘编号:
ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");
//网上有提到,用Win32_DiskDrive,但是用Win32_DiskDrive获得的硬盘信息中并不包含SerialNumber属性。
ManagementObjectCollection moc = mc.GetInstances();
string strID = null ;
foreach( ManagementObject mo in moc )
{
strID = mo.Properties["SerialNumber"].Value.ToString();
break;
}
textBox1.Text += "硬盘 ID:" + strID;
ManagementClass mc = new ManagementClass("Win32_PhysicalMedia");
//网上有提到,用Win32_DiskDrive,但是用Win32_DiskDrive获得的硬盘信息中并不包含SerialNumber属性。
ManagementObjectCollection moc = mc.GetInstances();
string strID = null ;
foreach( ManagementObject mo in moc )
{
strID = mo.Properties["SerialNumber"].Value.ToString();
break;
}
textBox1.Text += "硬盘 ID:" + strID;
返回结果:
电脑1:硬盘 ID:4833395344463658202020202020202020202020
电脑2:硬盘 ID:WD-WMAJD1092385
电脑3:硬盘 ID:4a353756354d5939202020202020202020202020
电脑4:硬盘 ID:0637J2FW508014
- 获取BIOS编号:
返回结果:
电脑1:BIOS ID:
电脑2:BIOS ID:CN24401483
电脑3:BIOS ID:
电脑4:BIOS ID:
总结:
由以上各步看出,通过Win32_Processor获取CPUID不正确,或者说Win32_Processor字段就不包含CPU编号信息。
通过Win32_BaseBoard获取主板信息,但不是所有的主板都有编号,或者说不是能获取所有系统主板的编号。
通过Win32_PhysicalMedia获取硬盘编号应该没有问题。但网上说可以通过Win32_DiskDrive获取,其实所得信息根本不包含SerialNumber。
通过Win32_BIOS获取BIOS信息,基本和获取主板信息差不多。就是说:不是所有的主板BIOS信息都有编号。
另外,可以将通过以上各字段所得信息输出,逐个查看所有信息 属性和对应的值。代码如下:
===============================================
首先要添加对System.Management.dll的引用
==============================================
首先需添加对System.Management的引用。
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');
}
}
}
使用方法举例:
==========
引用楼主的代码:
//定义DLL文件名,此文件路径要加到系统Path中
private const string _fileDll = @"CPUID_Util.dll";
//GetCPUID是 dll里面的一个方法
private const string _FountionName = @"GetCPUID";
[DllImport(_fileDll, EntryPoint = _FountionName, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
//C#中的申明
public static extern string GetCPUID(string id);
回复
//声明一个stringBuilder类型字符串
StringBuilder strcpu=new StringBuilder();
//将strcpu带入函数中
GetCPUID(strcpu);
//此时的strcpu已经是cpuid了 Console.writeLine(strcpu);
==================================
多核系统中每个处理器的ID是不同的,在哪个处理器上执行得到的就是哪个处理器的ID,获取ID之前可以用SetThreadAffinityMask指定在哪个处理器上执行,这样才能确保每次得到相同的结果。
最后
以上就是冷艳毛衣为你收集整理的C#获取电脑硬件信息(CPU ID、主板ID、硬盘ID、BIOS编号)的全部内容,希望文章能够帮你解决C#获取电脑硬件信息(CPU ID、主板ID、硬盘ID、BIOS编号)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复