如何从c#获取MAC地址

时间:2013-10-26 11:53:10

标签: c#

我有一个C#应用程序。在例程中,我有代码来使用SendARP获取MAC地址。

它适用于Windows 7,但不适用于Windows XP。我只是返回一个空字符串。

这是我的代码:

System.Runtime.InteropServices.DllImport("iphlpapi.dll", ExactSpelling = true)]
        static extern int SendARP(int DestIP, int SrcIP, byte[] pMacAddr, ref int PhyAddrLen);
        public static PhysicalAddress GetMacAddress(IPAddress ipAddress)
        {
            const int MacAddressLength = 6; //i know it is has a length of 6
            int length = MacAddressLength;
            var macBytes = new byte[MacAddressLength];
            SendARP(BitConverter.ToInt32(ipAddress.GetAddressBytes(), 0), 0, macBytes, ref length);
            return new PhysicalAddress(macBytes);
        }

编辑:附加信息 这些方法都不适合我。有问题的设备是IP摄像头。它位于不同子网上的固定IP地址〜192.168.100.100。 XP无法获取MAC地址,但在Windows 7上却可以。这些设备公开tvdevicedesc.xml,以便获取有关这些设备的元信息,我在我的代码中使用“http://”+ _device.Connection +“/ tvdevicedesc.xml”并解析数据。这给了我mac地址。如果有人不确定我的意思,那么请提出问题,我会进一步解释。

0 个答案:

没有答案
相关问题