C#如何获取客户端CPU,硬盘,MAC序列号

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#如何获取客户端CPU,硬盘,MAC序列号相关的知识,希望对你有一定的参考价值。

要客户端的。。怎么获取啊
这些硬件上面都有,你可以在硬件说明上找到这些序列号 。。。。

用C#做系统的。。。。哪会要人去看硬件。。我要获取客户端CPU,硬盘,MAC序列号

winform可以,webform几乎就不可能除非写插件,
这是在winform下的。客户端的

using System.Runtime.InteropServices;
using System.Management;
using System;

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);


//取网卡mac
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:X1,2:X2,2:X3,2:X4,2:X5,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');

参考技术A 如果是运行在本地的WinForm程序的话可以通过注册表来获取,但不能保证准确,因为用户有可能会改变注册表。 参考技术B 这些硬件上面都有,你可以在硬件说明上找到这些序列号 参考技术C 收藏坐等高的

如何使用 C# 获取硬盘序列号?

【中文标题】如何使用 C# 获取硬盘序列号?【英文标题】:How do I use C# to get the Hard-disk serial number? 【发布时间】:2010-11-24 03:26:41 【问题描述】:

如何在不使用dll且VISTA支持的情况下获取硬盘序列号

【问题讨论】:

【参考方案1】:
using System.Management;

public string GetHDDSerial()

    ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

    foreach (ManagementObject wmi_HD in searcher.Get())
    
        // get the hardware serial no.
        if (wmi_HD["SerialNumber"] != null)
            return wmi_HD["SerialNumber"].ToString();
    

    return string.Empty;

【讨论】:

【参考方案2】:

这是适合我的代码:

ManagementObjectSearcher searcher = null;
 searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

 foreach (ManagementObject wmi_HD in searcher.Get())
 
     HardDrive hd = new HardDrive();

     try
       
         txtmdl.Text = hd.Caption = wmi_HD["Caption"].ToString();
         txtsn.Text=(hd.SerialNo = wmi_HD.GetPropertyValue("SerialNumber").ToString());

我刚刚添加了 2 个文本框来获得结果,你也可以这样做!

和硬盘类:

public class HardDrive
        
            public string Model  get; set; 
            public string InterfaceType  get; set; 
            public string Caption  get; set; 
            public string SerialNo  get; set; 
        

别忘了使用:

using System.Management;

来源: get serial number of hard disk in c#

【讨论】:

【参考方案3】:

试试here 的这段代码,如果它有效,请告诉我们:

// Namespace Reference
using System.Management;

/// <summary>
/// method to retrieve the selected HDD's serial number
/// </summary>
/// <param name="strDriveLetter">Drive letter to retrieve serial number for</param>
/// <returns>the HDD's serial number</returns>
public string GetHDDSerialNumber(string drive)

    //check to see if the user provided a drive letter
    //if not default it to "C"
    if (drive == "" || drive == null)
    
        drive = "C";
    
    //create our ManagementObject, passing it the drive letter to the
    //DevideID using WQL
    ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive +":\"");
    //bind our management object
    disk.Get();
    //return the serial number
    return disk["VolumeSerialNumber"].ToString();

编辑:如果这不起作用,请尝试 CodeProject 中的这段代码:

首先,让我们创建一个类来存储有关硬盘的信息:

class HardDrive

 private string model = null;
 private string type = null;
 private string serialNo = null; 
 public string Model
 
  get return model;
  set model = value;
  
 public string Type
 
  get return type;
  set type = value;
  
 public string SerialNo
 
  get return serialNo;
  set serialNo = value;
 

接下来,我们查询 Win32_DiskDrive 类:

ManagementObjectSearcher searcher = new
 ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

foreach(ManagementObject wmi_HD in searcher.Get())

 HardDrive hd = new HardDrive();
 hd.Model = wmi_HD["Model"].ToString();
 hd.Type  = wmi_HD["InterfaceType"].ToString();
 hdCollection.Add(hd);

现在我们需要从 Win32_PhysicalMedia 中提取序列号 类:

searcher = new
 ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

int i = 0;
foreach(ManagementObject wmi_HD in searcher.Get())

 // get the hard drive from collection
 // using index
 HardDrive hd = (HardDrive)hdCollection[i];

 // get the hardware serial no.
 if (wmi_HD["SerialNumber"] == null)
  hd.SerialNo = "None";
 else
  hd.SerialNo = wmi_HD["SerialNumber"].ToString();

 ++i;

现在我们显示我们的硬盘信息:

// Display available hard drives
foreach(HardDrive hd in hdCollection)

 Console.WriteLine("Model\t\t: " + hd.Model);
 Console.WriteLine("Type\t\t: " + hd.Type);
 Console.WriteLine("Serial No.\t: " + hd.SerialNo);
 Console.WriteLine();

【讨论】:

【参考方案4】:

您好,刚刚找到此链接

它对我有用:

enter link description here

这是代码的基本部分:

  /// <summary>
  /// return Volume Serial Number from hard drive
  /// </summary>
  /// <param name="strDriveLetter">[optional] Drive letter</param>
  /// <returns>[string] VolumeSerialNumber</returns>
  public string GetVolumeSerial(string strDriveLetter)
  
      if( strDriveLetter=="" || strDriveLetter==null) 
          strDriveLetter="C";
     ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + strDriveLetter +":\"");
     disk.Get();
     return disk["VolumeSerialNumber"].ToString();
  

【讨论】:

以上是关于C#如何获取客户端CPU,硬盘,MAC序列号的主要内容,如果未能解决你的问题,请参考以下文章

用vs2012编写c程序,如何获得CPU ID、MAC地址和硬盘序列号

获取网卡MAC硬盘序列号CPU_IDBIOS编号

Python获取电脑硬件配置的封装类,可以获取CPU序列号主板序列号BIOS序列号硬盘序列号和网卡MAC地址

Python获取电脑硬件配置的封装类,可以获取CPU序列号主板序列号BIOS序列号硬盘序列号和网卡MAC地址

Python获取电脑CPU序列号主板序列号BIOS序列号硬盘序列号列表网卡MAC地址

使用WMI编程获取主机硬件信息(CPU_ID,硬盘主板BIOS序列号,Mac地址)