Reputation: 25
I use this code to get the available IPv4 addresses:
static void Main(string[] args)
{
string host = System.Net.Dns.GetHostName();
System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(host);
System.Net.IPAddress[] ipAddr = ipEntry.AddressList;
for (int i = 0; i < ipAddr.Length; i++)
{
if (ipAddr[i].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
Console.WriteLine( ipAddr[i]);
}
}
For my machine this currently gives:
192.168.1.11
192.168.240.1
192.168.182.1
10.1.1.121
whereas 192.168.1.11 is my network adapter, the next two are from VMware Network, and 10.1.1.121 is from a currently active OpenVPN connection.
How can I reliably detect the IPv4 address 192.168.1.11 (= network adapter) only? I guess that it's just incidentally on the first place.
Thanks, Robert
Upvotes: 1
Views: 14265
Reputation: 2276
This is the code I used:
private string getLocalIP()
{
string Localip = "?";
foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
var defaultGateway = from nics in NetworkInterface.GetAllNetworkInterfaces()
from props in nics.GetIPProperties().GatewayAddresses
where nics.OperationalStatus == OperationalStatus.Up
select props.Address.ToString(); // this sets the default gateway in a variable
GatewayIPAddressInformationCollection prop = netInterface.GetIPProperties().GatewayAddresses;
if(defaultGateway.First() != null){
IPInterfaceProperties ipProps = netInterface.GetIPProperties();
foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses)
{
if (addr.Address.ToString().Contains(defaultGateway.First().Remove(defaultGateway.First().LastIndexOf(".")))) // The IP address of the computer is always a bit equal to the default gateway except for the last group of numbers. This splits it and checks if the ip without the last group matches the default gateway
{
if (Localip == "?") // check if the string has been changed before
{
Localip = addr.Address.ToString(); // put the ip address in a string that you can use.
}
}
}
}
}
return Localip;
}
Upvotes: 2
Reputation: 737
As @Nappy Mentions using NetworkInterface is nicer approach. Quick sample below.
private IEnumerable<IPAddress> GetIpsForNetworkAdapters()
{
var nics = from i in NetworkInterface.GetAllNetworkInterfaces()
where i.OperationalStatus == OperationalStatus.Up
select new { name = i.Name, ip = GetIpFromUnicastAddresses(i) };
return nics.Select(x => x.ip);
}
private IPAddress GetIpFromUnicastAddresses(NetworkInterface i)
{
return (from ip in i.GetIPProperties().UnicastAddresses
where ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork
select ip.Address).SingleOrDefault();
}
Upvotes: 2
Reputation: 3046
The answer you got is not entirely true, because those four IPv4 adresses in your example all belong to a network adapter, even if they might be virtual only.
To get more information about your network interfaces, you should check the NetworkInterface Class or WMI. You can filter out by type to remove loopback and tunnel interfaces for example.
Which network adapter is actually used is as far as I know dependent on the destination address of the packet you want to send. The network adapters use the Adress Resolution Protocol to check if they can reach the desination IP, and the MAC address of the gateway.
So short answer; there is no default network adapter.
Upvotes: 9
Reputation: 68536
Try this
System.Net.Dns.GetHostByName(Environment.MachineName).AddressList[0].ToString();
Upvotes: -1