在C#中如何查找内部设备的mac地址而不是Router mac地址

时间:2018-03-05 10:01:27

标签: c#

由于某些安全原因,我想在网络上获取我的软件用户的mac地址。但我可以通过以下方法获取路由器的mac地址。

public string GetMACAddress()
    {
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        String sMacAddress = string.Empty;
        foreach (NetworkInterface adapter in nics)
        {
            if (sMacAddress == String.Empty)// only return MAC Address from first card  
            {
                IPInterfaceProperties properties = adapter.GetIPProperties();
                sMacAddress = adapter.GetPhysicalAddress().ToString();
            }
        } return sMacAddress;
    }

我需要在互联网上访问我的软件的系统的特定mac地址以及该路由器的内部设备。那可能通过C#?

3 个答案:

答案 0 :(得分:2)

这对我有用。!

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function($scope) {
  $scope.data = {
    id: 1,
    meetingName: "Meeting 1",
    meetingDate: "2018-02-21",
    startTime: "10:00:00"
  };
});

myApp.filter('timeFilter', function ($filter) {
  return function (data, aDate, dateFilter) {
    return $filter('date')(new Date(aDate + " " + data), dateFilter);
  }
})

答案 1 :(得分:1)

static string GetMacAddress()
{
    string macAddresses = "";
    foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces()) {
        // Only consider Ethernet network interfaces, thereby ignoring any
        // loopback devices etc.
        if (nic.NetworkInterfaceType != NetworkInterfaceType.Ethernet) continue;
        if (nic.OperationalStatus == OperationalStatus.Up) {
            macAddresses += nic.GetPhysicalAddress().ToString();
            break;
        }
    }
    return macAddresses;
}

取自here,只需一个快速谷歌。

答案 2 :(得分:1)

此方法是获取用户的Mac地址的最佳方式。

[DllImport("Iphlpapi.dll")]
        private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
        [DllImport("Ws2_32.dll")]
        private static extern Int32 inet_addr(string ip);
        private static string GetClientMAC(string strClientIP)
        {
            string mac_dest = "";
            try
            {
                Int32 ldest = inet_addr(strClientIP);
                Int32 lhost = inet_addr("");
                Int64 macinfo = new Int64();
                Int32 len = 6;
                int res = SendARP(ldest, 0, ref macinfo, ref len);
                string mac_src = macinfo.ToString("X");

                while (mac_src.Length < 12)
                {
                    mac_src = mac_src.Insert(0, "0");
                }

                for (int i = 0; i < 11; i++)
                {
                    if (0 == (i % 2))
                    {
                        if (i == 10)
                        {
                            mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
                        }
                        else
                        {
                            mac_dest = "-" + mac_dest.Insert(0, mac_src.Substring(i, 2));
                        }
                    }
                }
            }
            catch (Exception err)
            {
                throw new Exception("Lỗi " + err.Message);
            }
            return mac_dest;
        }