如何使用C#获得PC的100%真实IP和MAC地址

时间:2018-06-05 08:47:14

标签: c# ip

我正在获取用于套接字连接的PC的IP和MAC,但问题是在PC中有2 3个适用于网络接口的适配器,比如我有Wi-Fi和LAN也因此有时会提供错误的IP地址和我的套接字连接出错了。

以下是获取IP地址的代码

public static string GetIPAddress()
{
    string Response = string.Empty;
    try
    {
        var ni = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface item in ni)
        {
            if (item.OperationalStatus == OperationalStatus.Up) //&& item.NetworkInterfaceType == ?
            {
                foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses)
                {
                    if (ip.Address.AddressFamily == AddressFamily.InterNetwork & !IPAddress.IsLoopback(ip.Address))
                    {
                        Response = ip.Address.ToString();
                    }
                }
            }
        }
    }
    catch (Exception)
    {

    }
    return Response;
}

1 个答案:

答案 0 :(得分:1)

您可能正在阅读IPv6地址 或者,如果以太网接口OperationalStatus已启动但未连接 - 可能是因为缺少DHCP引用,则具有链路层Suffix的自动专用IP地址(例如169.254.0.0→{{1 }})。

确定IP地址是否为工作 IP的一种可能方法是测试IsDnsEligible类的UnicastIPAddressInformation属性,可以使用{{}访问该属性3}} NetworkInterface方法。

这将排除所有不可路由的地址(私有,环回等) 此外,如果您知道您的连接必须使用特定传输(以太网,WiFi,隧道,Atm等),您还可以测试169.254.80.104 GetIPProperties()属性。请参阅枚举值:NetworkInterfaceType

NetworkInterface

现在,您可以识别过滤列表的有效/可路由IP地址:

using System.Net.NetworkInformation;

NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();

//Get all the IPv4 addresses
List<NetworkInterface> IPV4Interfaces = Interfaces
    .Where(IF => (IF.Supports(NetworkInterfaceComponent.IPv4)))
    .ToList();

//Utility list which collects informations on all the IPv4 interfaces
List<NetWorkInterfacesInfo> NetInterfacesInfo = IPV4Interfaces.Select(IF => new NetWorkInterfacesInfo()
{
    Name = IF.Name,
    Description = IF.Description,
    Status = IF.OperationalStatus,
    Speed = IF.Speed,
    InterfaceType = IF.NetworkInterfaceType,
    MAC = IF.GetPhysicalAddress(),
    DnsServers = IF.GetIPProperties().DnsAddresses.Select(ipa => ipa).ToList(),
    IPAddresses = IF.GetIPProperties().UnicastAddresses.Select(ipa => ipa.Address).ToList(),
    Gateways = IF.GetIPProperties().GatewayAddresses.Select(ipa => ipa.Address).ToList(),
    DHCPEnabled = IF.GetIPProperties().GetIPv4Properties().IsDhcpEnabled,
    BytesSent = IF.GetIPStatistics().BytesSent,
    BytesReceived = IF.GetIPStatistics().BytesReceived
}).ToList();


//Utility container class for the NetInterfacesInfo list
public class NetWorkInterfacesInfo
{
    public string Name { get; set; }
    public string Description { get; set; }
    public PhysicalAddress MAC { get; set; }
    public List<IPAddress> IPAddresses { get; set; }
    public List<IPAddress> DnsServers { get; set; }
    public List<IPAddress> Gateways { get; set; }
    public bool DHCPEnabled { get; set; }
    public OperationalStatus Status { get; set; }
    public NetworkInterfaceType InterfaceType { get; set; }
    public Int64 Speed { get; set; }
    public Int64 BytesSent { get; set; }
    public Int64 BytesReceived { get; set; }
}

或者使用更多过滤器来选择,例如,使用无线传输的所有可路由IP地址:

//Extract the Dns Eligible IP addresses
if (IPV4Interfaces != null)
{
    List<UnicastIPAddressInformation> RoutableIpAddresses =
        IPV4Interfaces.Select(IF => IF.GetIPProperties().UnicastAddresses.Last())
                      .Where(UniIP => UniIP.IsDnsEligible).ToList();
}