分享
 
 
 

[收藏]C#如何获得cpu,硬盘的物理序列号

王朝c#·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

using System;

using System.Runtime.InteropServices;

using System.Management;

namespace Hardware

{

/// <summary>

/// Hardware_Mac 的摘要说明。

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

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有