当前位置: 移动技术网 > IT编程>开发语言>c# > C#获取机器码的方法详解(机器名,CPU编号,硬盘编号,网卡mac等)

C#获取机器码的方法详解(机器名,CPU编号,硬盘编号,网卡mac等)

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了c#获取机器码的方法。分享给大家供大家参考,具体如下:

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

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#窗体操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#程序设计之线程使用技巧总结》、《c#操作excel技巧总结》、《c#中xml文件操作技巧汇总》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程

希望本文所述对大家c#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网