如何使用C#获得PC的100%真实IP和MAC地址 [英] How i can get 100% real IP and MAC address of PC using C#
问题描述
我正在获取用于套接字连接的PC的IP和MAC,但问题是在PC中有2个3适配器用于网络接口,比如我有Wi-Fi和LAN也因此有时它提供了错误的IP地址和我的套接字连接错误。
I am getting IP and MAC of the PC for socket connection but the problem is in PC there are 2 3 adapters for network interface like I have Wi-fi and LAN also so sometime it's giving wrong IP Address and my socket connection goes wrong.
以下是获取IP地址的代码
Following is my code to get IP Address
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;
}
推荐答案
你可能正在阅读 IPv6
地址。
或者,如果以太网接口 OperationalStatus
已启用但未连接 - 可能是因为丢失的DHCP参考,带链接层的自动专用IP地址后缀(例如 169.254.0.0
→ 169.254.80.104
)。
You are probably reading the IPv6
address.
Or, if the Ethernet interface OperationalStatus
is Up but not connected - possibly because of a missing DHCP reference, the Automatic Private IP Address with a Link Layer Suffix (e.g. 169.254.0.0
→ 169.254.80.104
).
确定IP地址是否为工作 IP的一种可能方法是测试 IsDnsEligible
属性0%29.aspx?f = 255& MSPPError = -2147217396rel =nofollow noreferrer> UnicastIPAddressInformation 类,可以使用 NetworkInterface GetIPProperties()
方法。
One possible way to determine whether the IP Address is a working IP, is testing the IsDnsEligible
property of the UnicastIPAddressInformation class, which can be accessed using the NetworkInterface GetIPProperties()
method.
这将排除所有不可路由的地址(私有,环回等)。
另外,如果你知道的话您的连接必须使用特定的传输(以太网,WiFi,隧道,Atm等),您还可以测试 NetworkInterface
NetworkInterfaceType 属性。请参阅枚举值: NetworkInterfaceType Enumeration 。
This will exclude all non-routable addresses (Private, Loopback etc.).
Also, if you know that your connection will have to use a specific transport (Ethernet, WiFi, Tunnel, Atm etc.), you can also test the NetworkInterface
NetworkInterfaceType property. See the enumeration values: NetworkInterfaceType Enumeration.
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地址:< br>
Now you can identify a valid/routable IP Address filtering the list:
//Extract the Dns Eligible IP addresses
if (IPV4Interfaces != null)
{
List<UnicastIPAddressInformation> RoutableIpAddresses =
IPV4Interfaces.Select(IF => IF.GetIPProperties().UnicastAddresses.Last())
.Where(UniIP => UniIP.IsDnsEligible).ToList();
}
或者使用更多过滤器来选择,例如,所有可路由的IP地址无线传输:
Or use more filters to select, for example, all routable IP Addresses with a Wireless transport:
if (IPV4Interfaces != null)
{
List<UnicastIPAddressInformation> RoutableIpAddresses =
IPV4Interfaces.Where(IF => IF.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
.Select(IF => IF.GetIPProperties().UnicastAddresses.Last())
.Where(UniIP => UniIP.IsDnsEligible).ToList();
}
这篇关于如何使用C#获得PC的100%真实IP和MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!