基本信息
源码名称:c#实现取局域网内根据MAC地址获取IP地址
源码大小:0.04M
文件格式:.rar
开发语言:C#
更新时间:2017-05-23
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 5 元 
   源码介绍



using System;
using System.Collections;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;

namespace GetMACorIPHelper
{
  public static  class GetMACorIP
    {
        [DllImport("ws2_32.dll")]
        private static extern int inet_addr(string cp);

        [DllImport("IPHLPAPI.dll")]
        private static extern int SendARP(Int32 DestIP, Int32 SrcIP, ref Int64 pMacAddr, ref Int32 PhyAddrLen);

        /// <summary>
        /// 根据ip地址获得MAC地址
        /// </summary>
        /// <param name="hostip"></param>
        /// <returns></returns>
        public static string GetMacAddress(string hostip)//获取远程IP(不能跨网段)的MAC地址
        {
            string Mac = "";
            try
            {
                Int32 ldest = inet_addr(hostip); //将IP地址从 点数格式转换成无符号长整型
                Int64 macinfo = new Int64();
                Int32 len = 6;
                SendARP(ldest, 0, ref macinfo, ref len);
                string TmpMac = Convert.ToString(macinfo, 16).PadLeft(12, '0');//转换成16进制  注意有些没有十二位
                Mac = TmpMac.Substring(0, 2).ToUpper();//
                for (int i = 2; i < TmpMac.Length; i = i   2)
                {
                    Mac = TmpMac.Substring(i, 2).ToUpper()   "-"   Mac;
                }
            }
            catch (Exception Mye)
            {
                Mac = "获取远程主机的MAC错误:"   Mye.Message;
            }
            return Mac;
        }




        /// <summary>
        /// 获得所有的IP 和 MAC
        /// </summary>
        public static Dictionary<string, string> GetAllIPAndMac()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();

            DirectoryEntry entryPC = new DirectoryEntry("WinNT:");

            foreach (DirectoryEntry child in entryPC.Children)
            {
                foreach (DirectoryEntry pc in child.Children)
                {
                    try
                    {
                        IPHostEntry hostent = Dns.GetHostByName(pc.Name); // 主机信息
                        Array addrs = hostent.AddressList;            // IP地址数组
                        IEnumerator it = addrs.GetEnumerator();       // 迭代器
                        while (it.MoveNext())
                        {                     // 循环到下一个IP 地址
                            IPAddress ip = (IPAddress)it.Current;      // 获得 IP 地址
                            string mac = GetMacAddress(ip.ToString()); //获得MAC地址
                            dic.Add(ip.ToString(), mac);
                            Console.WriteLine(ip   " "   mac);
                        }
                    }
                    catch (Exception ex)
                    {
                        //Console.WriteLine(ex);
                    }

                }
            }

            return dic;
        }

        /// <summary>
        /// 根据MAC地址获得IP地址
        /// </summary>
        /// <param name="macaddr"></param>
        /// <returns></returns>
        public static string GetIPAddress(string macaddr)
        {
            string ipAddr = "";

            DirectoryEntry entryPC = new DirectoryEntry("WinNT:");

            foreach (DirectoryEntry child in entryPC.Children)
            {
                foreach (DirectoryEntry pc in child.Children)
                {
                    try
                    {
                        IPHostEntry hostent = Dns.GetHostByName(pc.Name); // 主机信息
                        Array addrs = hostent.AddressList;            // IP地址数组
                        IEnumerator it = addrs.GetEnumerator();       // 迭代器
                        while (it.MoveNext())
                        {                     // 循环到下一个IP 地址
                            IPAddress ip = (IPAddress)it.Current;      // 获得 IP 地址
                            string mac = GetMacAddress(ip.ToString()).ToLower(); //获得MAC地址
                            if (mac == macaddr)
                            {
                                ipAddr = ip.ToString();
                                //Console.WriteLine(ip   " "   mac);
                                break;
                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        //Console.WriteLine(ex);
                    }

                    if (ipAddr != "")
                    {
                        break;
                    }
                }

                if (ipAddr != "")
                {
                    break;
                }
            }

            return ipAddr;
        }
    }
}