I need to find out the local Ip address, for that reason I was using the following code:
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
I find out that when a PC has more than one IP with AddressFamily.InterNetwork
I get always the first one. However I can't find any property to find out the active IP.
How can I get the correct IP?
Thanks for any tip!
I have picked this up from internet a while ago.
string strHostName = System.Net.Dns.GetHostName(); ;
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
This should give you an array of all the ip addresses of your pc.
Bwall has a fitting solution posted in this thread.
foreach(NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
if(ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
Console.WriteLine(ni.Name);
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
Console.WriteLine(ip.Address.ToString());
}
}
}
}
The important thing in this piece of code is, that it only lists IP addresses of ethernet and wireless interfaces. I doubt you'll have a serial connection active, so this won't matter most likely. Alternativly you can always edit the if statement.
//EDIT
If you only want to IP address which actually connects to the internet use Hosam Aly's solution
This is his code:
static IPAddress getInternetIPAddress()
{
try
{
IPAddress[] addresses = Dns.GetHostAddresses(Dns.GetHostName());
IPAddress gateway = IPAddress.Parse(getInternetGateway());
return findMatch(addresses, gateway);
}
catch (FormatException e) { return null; }
}
static string getInternetGateway()
{
using (Process tracert = new Process())
{
ProcessStartInfo startInfo = tracert.StartInfo;
startInfo.FileName = "tracert.exe";
startInfo.Arguments = "-h 1 www.example.com
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
tracert.Start();
using (StreamReader reader = tracert.StandardOutput)
{
string line = "";
for (int i = 0; i < 5; ++i)
line = reader.ReadLine();
line = line.Trim();
return line.Substring(line.LastIndexOf(' ') + 1);
}
}
}
static IPAddress findMatch(IPAddress[] addresses, IPAddress gateway)
{
byte[] gatewayBytes = gateway.GetAddressBytes();
foreach (IPAddress ip in addresses)
{
byte[] ipBytes = ip.GetAddressBytes();
if (ipBytes[0] == gatewayBytes[0]
&& ipBytes[1] == gatewayBytes[1]
&& ipBytes[2] == gatewayBytes[2])
{
return ip;
}
}
return null;
}
What it basically does, is trace the route to www.example.com and processes the right IP from there. I tested the code on my machine and needed to change the iterations from 9 to 5 to get the right line from stream. You better recheck it or you might into a NullReferenceException because line
will be null
.
static string GetActiveIP()
{
string ip = "";
foreach (NetworkInterface f in NetworkInterface.GetAllNetworkInterfaces())
{
if (f.OperationalStatus == OperationalStatus.Up)
{
IPInterfaceProperties ipInterface = f.GetIPProperties();
if (ipInterface.GatewayAddresses.Count > 0)
{`enter code here`
foreach (UnicastIPAddressInformation unicastAddress in ipInterface.UnicastAddresses)
{
if ((unicastAddress.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) && (unicastAddress.IPv4Mask.ToString() != "0.0.0.0"))
{
ip = unicastAddress.Address.ToString();
break;
}
}`enter code here`
}
}
}
return ip;
}