C#如何获得设备Mac地址_第1页
C#如何获得设备Mac地址_第2页
C#如何获得设备Mac地址_第3页
C#如何获得设备Mac地址_第4页
C#如何获得设备Mac地址_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C#如何获得设备Mac地址 利用dns类和WMI规范获取IP及MAC地址在C#编程中,要获取主机名和主机IP地址,是比较容易的.它提供的Dns类,可以轻松的取得主机名和IP地址.示例:c-sharp view plaincopy1. string strHostName = Dns.GetHostName(); /得到本机的主机名  2. IPHostEntry ipEntry = Dns.GetHostByName(strHostName); /取得本机IP  3. str

2、ing strAddr = ipEntry.AddressList0.ToString(); /假设本地主机为单网卡  在这段代码中使用了两个类,一个是Dns类,另一个为IPHostEntry类,二者都存在于命名空间System.Net中.Dns类主要是从域名系统(DNS)中检索关于特定主机的信息,上面的代码第一行就从本地的DNS中检索出本地主机名.IPHostEntry类则将一个域名系统或主机名与一组IP地址相关联,它与DNS类一起使用,用于获取主机的IP地址组.要获取远程主机的IP地址,其方法也是大同小异.在获取了IP地址后,如果

3、还需要取得网卡的MAC地址,就需要进一步探究了.这里又分两种情况,一是本机MAC地址,二是远程主机MAC地址.二者的获取是完全不同的.在获取本机的MAC地址时,可以使用WMI规范,通过SELECT语句提取MAC地址.在.NET框架中,WMI规范的实现定义在System.Management命名空间中.ManagementObjectSearcher类用于根据指定的查询检索管理对象的集合ManagementObjectCollection类为管理对象的集合,下例中由检索对象返回管理对象集合赋值给它.示例:c-sharp view plaincopy1. ManagementObjectSearc

4、her query =new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration")   2. ManagementObjectCollection queryCollection = query.Get();  3. foreach( ManagementObject mo in queryColle

5、ction )   4.    5.    if(mo"IPEnabled".ToString() = "True")  6.    mac = mo"MacAddress".ToString();  7.    获取远程主机的MAC地址时,需要借用API函数SendARP.该函数使用ARP协议,向

6、目的主机发送ARP包,利用返回并存储在高速缓存中的IP和MAC地址对,从而获取远程主机的MAC地址.示例:c-sharp view plaincopy1. Int32 ldest= inet_addr(remoteIP); /目的ip   2. Int32 lhost= inet_addr(localIP); /本地ip   3.   4. try   5.    6.   &

7、#160;Int64 macinfo = new Int64();   7.    Int32 len = 6;   8.    int res = SendARP(ldest,0, ref macinfo, ref len); /发送ARP包  9.    return

8、0;Convert.ToString(macinfo,16);   10.    11. catch(Exception err)   12.    13.    Console.WriteLine("Error:0",err.Message);   14.    15. return 0.ToString();   

9、但使用该方式获取MAC时有一个很大的限制,就是只能获取同网段的远程主机MAC地址.因为在标准网络协议下,ARP包是不能跨网段传输的,故想通过ARP协议是无法查询跨网段设备MAC地址的。示例程序:c-sharp view plaincopy1. using System.Net;  2. using System;  3. using System.Management;  4. using System.Runtime.InteropServices;  5.  &

10、#160;6. namespace ConsoleApplication2  7.   8.     public class Program  9.       10.         DllImport("Iphlpapi.dll")  11.    &#

11、160;    private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);  12.         DllImport("Ws2_32.dll")  13. 

12、60;       private static extern Int32 inet_addr(string ip);  14.   15.         /获取本机的IP  16.         public string getLo

13、calIP()  17.           18.             string strHostName = Dns.GetHostName(); /得到本机的主机名  19.          &#

14、160;  IPHostEntry ipEntry = Dns.GetHostByName(strHostName); /取得本机IP  20.             string strAddr = ipEntry.AddressList0.ToString();  21.       

15、;      return (strAddr);  22.           23.         /获取本机的MAC  24.         public string getLocalMac()

16、60; 25.           26.             string mac = null;  27.             /DllImport("System.Manage

17、ment.dll")  28.             ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");  29.     

18、;        ManagementObjectCollection queryCollection = query.Get();  30.             foreach (ManagementObject mo in queryCollection)  31.  &

19、#160;            32.                 if (mo"IPEnabled".ToString() = "True")  33.      

20、0;              mac = mo"MacAddress".ToString();  34.               35.          

21、0;  return (mac);  36.           37.   38.         /获取远程主机IP  39.         public string getRemoteIP(string Rem

22、oteHostName)  40.          41.         IPHostEntry ipEntry = Dns.GetHostByName(RemoteHostName);  42.         IPAddress IpAddr =

23、60;ipEntry.AddressList;  43.         string strAddr = new stringIpAddr.Length;  44.         for (int i=0;i<strAddr.Length;i+)  45.     &#

24、160;     46.          strAddri = IpAddri.ToString();  47.           48.         return(strAddr);  49.  &#

25、160;       50.         /获取远程主机MAC  51.         public string getRemoteMac(string localIP, string remoteIP)  52.     

26、0;     53.             Int32 ldest = inet_addr(remoteIP); /目的ip   54.             Int32 lhost = inet_addr

27、(localIP); /本地ip   55.   56.             try  57.               58.           &#

28、160;     Int64 macinfo = new Int64();      59.                 Int32 len = 6;      60.   &

29、#160;             int res = SendARP(ldest,0, ref macinfo, ref len);      61.                

30、60;string macnum = Convert.ToString(macinfo, 16);/16进制的Mac地址数据  62.                 string macArr = new stringmacnum.Length;  63.      

31、           string macaddr=""  64.                 for (int i = 0; i < (macnum.Length/2); i+) 

32、; 65.                   66.                     macArri = macnummacnum.Length - 2*(i+

33、1).ToString()+macnummacnum.Length-2*i-1.ToString();/将Mac地址顺序转置  67.                   68.                 for (int&

34、#160;j=0; j< 6;j+)  69.                   70.                     if(j=0)  71

35、.                       72.                         macaddr =

36、0;macArr0.ToString();  73.                       74.                     else

37、60; 75.                       76.                         macaddr

38、 = macaddr+":"+macArrj.ToString();  77.                       78.                

39、0;  79.                 return macaddr;  80.               81.           &#

40、160; catch (Exception err)  82.               83.                 Console.WriteLine("Error:0", err.Message);&#

41、160; 84.               85.             return "无法获得MAC地址"  86.           87.   8

42、8.   89.         public static void Main(string args)  90.          91.             Program gi = new

43、 Program();  92.             Console.WriteLine("本地网卡信息:");  93.             Console.WriteLine(gi.getLocalIP() + " - &quo

44、t; + gi.getLocalMac();  94.              95.             Console.WriteLine("/n/r远程设备的网卡信息:");  96.            

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论