I am using the below code to retrieve IPv4 Address (Ethernet Adapter Local Area Connection). But i see multiple addresses being returned by the code. Is there a way to say which of the returned addresses would be Ethernet Adapter Local Area Connection IP Address ?
List<string> all = new List<string>();
string hostName = Dns.GetHostName();
IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
foreach (IPAddress address in hostEntry.AddressList)
{
if (address.AddressFamily == AddressFamily.InterNetwork)
all.Add(address.ToString() + "\n");
}
dataGridView2.DataSource = all.Select(x => new { Value = x }).ToList();
}
currently the above code returns 3 values
Ethernet Adapter Local Area Connection IP Address is 172.20. * ** . ***
Are you maybe looking for a connection of properties NetworkInterfaceType and AddressFamily?
foreach (var i in NetworkInterface.GetAllNetworkInterfaces())
{
Console.WriteLine("{0} ({1})", i.Name, i.NetworkInterfaceType);
foreach (var a in i.GetIPProperties().UnicastAddresses)
{
Console.WriteLine(" {0} ({1})", a.Address, a.Address.AddressFamily);
}
}
this will give you for example:
Local Area Connection (Ethernet) (InterNetworkV6) 169.254.171.253 (InterNetwork)
Assuming only one connection of interest with one IPv4 address, consider something like this:
var connectionName = "Ethernet Adapter Local Area Connection";
var connection =
NetworkInterface.GetAllNetworkInterfaces().FirstOrDefault(
ni => ni.Name == connectionName);
if (connection != null)
{
Console.WriteLine("Connection \"{0}\" found.", connectionName);
// Use a regex to focus on the IPv4 address and ignore the IPv6 address if
// present.
var ipV4Regex = new Regex("(?<IPv4Address>([0-9]{1,3}\\.){3}[0-9]{1,3})");
var unicastAddresses = connection.GetIPProperties().UnicastAddresses;
var ipV4Address =
unicastAddresses.FirstOrDefault(
ua => ipV4Regex.Match(ua.Address.ToString()).Success);
if (ipV4Address != null)
Console.WriteLine("IPv4 address {0} found.", ipV4Address.Address);
else
Console.WriteLine("IPv4 address not found.");
}
else
{
Console.WriteLine("Connection \"{0}\" not found.", connectionName);
}
I used console output for clarity, but you should find this adaptable to assigning dataGridView2.DataSource
.
{
var sb = new StringBuilder();
NetworkInterface[] networkInterfaces =
NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface network in networkInterfaces)
{
IPInterfaceProperties properties = network.GetIPProperties();
foreach (UnicastIPAddressInformation address in
properties.UnicastAddresses)
{
if (address.Address.AddressFamily != AddressFamily.InterNetwork) // We're only interested in IPv4 addresses for now
continue;
if (IPAddress.IsLoopback(address.Address)) // Ignore loopback addresses (e.g., 127.0.0.1)
continue;
if (network.OperationalStatus == OperationalStatus.Up)
sb.AppendLine(address.Address.ToString() + " (" + network.Name + ")");
}
}
Console.WriteLine(sb.ToString());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.