C#如何取硬件标志代码

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

时间: 2024-12-23 09:13:27

C#如何取硬件标志代码的相关文章

C#如何取硬件标志

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

C#如何取硬件标志_C#教程

using System; using System.Runtime.InteropServices; using  System.Management; namespace Hardware { /// <summary> /// Hardware_Mac 的摘要说明. /// </summary> public class HardwareInfo {   //取机器名    public string GetHostName()   {    return System.Ne

asp下实现记录集内随机取记录的代码_应用技巧

记录集内随机取记录的代码 <%  ' Moving to random record - Steven Jones' Extension If Not(记录集名称.bof and 记录集名称.eof) Then ' reset the cursor to the beginning If (记录集名称.CursorType > 0) Then 记录集名称.MoveFirst Else 记录集名称.Requery End If 记录集名称_totalrn = -1 记录集名称_totalrn =

抓取Google天气预报代码

  本款程序是一款简单的抓取Google天气预报代码哦,以前写的都是php,asp今天我发布一款jsp获取goolge天气代码哦.   public NodeList getWeatherDiv(String htmlUrl) {         NodeList res = null;         try{             Parser parser = new Parser(htmlUrl);             parser.setEncoding("GBK");

asp下实现记录集内随机取记录的代码

记录集内随机取记录的代码 <%  ' Moving to random record - Steven Jones' Extension If Not(记录集名称.bof and 记录集名称.eof) Then ' reset the cursor to the beginning If (记录集名称.CursorType > 0) Then 记录集名称.MoveFirst Else 记录集名称.Requery End If 记录集名称_totalrn = -1 记录集名称_totalrn =

php抓取页面与代码解析 推荐_php技巧

得到数据我们不能直接输出,往往需要对内容进行提取,然后再进行格式化,以更加友好的方式显现出来.下面先简单说一下本文的主要内容: 一. PHP抓取页面的主要方法: 1. file()函数 2. file_get_contents()函数 3. fopen()->fread()->fclose()模式 4.curl方式 5. fsockopen()函数 socket模式 6. 使用插件(如:http://sourceforge.net/projects/snoopy/) 二.PHP解析html或x

批处理随机取值练习代码_DOS/BAT

出题目的: 掌握利用random进行随机取值技巧 解题要求: 代码通用.高效 尽量简洁 尽量不生成临时文件 加分规则: 1 思路独特基准分5分 2 代码高效.通用基准分4分 3 技巧高超基准分3分 4 代码简洁基准分2分 5 完美代码加分15分 题目如下: 已知有1-26与a-z的一一对应,如1对应a 10对应j 26对应z,要求用批处理 从1-26 a-z这52个值中(全视为值)随机抽取10个互不相同的值并显示出来, 注意如同1和a 24和x 26和z这样的取值均视为相同不合要求. 解题限制:

jquery 表单取值常用代码_jquery

jquery操作表单元素代码/*假设在一个表单中有一个按钮id="save"$(document).ready(function(){     $("#save").click(function(){        $("#save").attr("disabled",true);//设为不可用                                 $("#form1")[0].submit();

使用phantomjs进行网页抓取的实现代码_javascript技巧

phantomjs因为是无头浏览器可以跑js,所以同样可以跑dom节点,用来进行网页抓取是再好不过了. 比如我们要批量抓取网页 "历史上的今天" 的内容.网站 对dom结构的观察发现,我们只需要取到 .list li a的title值即可.因此我们利用高级选择器构建dom片段 var d= '' var c = document.querySelectorAll('.list li a') var l = c.length; for(var i =0;i<l;i++){ d=d+