G
Guest
How to obtain the IP address of the interface that is connected to the
Internet?
I´m using the code below to get IP address of the local machine:
//*********************************************
private void button1_Click(object sender, System.EventArgs e)
{
string HostName = Dns.GetHostName();
lblHostName.Text=HostName;
IPHostEntry ipEntry = Dns.GetHostByName (HostName);
IPAddress [] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
if(ipEntry.AddressList.AddressFamily
listBox1.Items.Add("IP:"+ i.ToString() + " " + addr.ToString ());
}
}
//*********************************************
The code is fine, but when I run the program, I receive a list of various
IPs due to the interfaces that Ihave on this machines. I know what is the
address that I´m using to connect to the Internet, but how to do this using
code? It is always the last addres on the list?
Thanks in advance,
Rodrigo
Internet?
I´m using the code below to get IP address of the local machine:
//*********************************************
private void button1_Click(object sender, System.EventArgs e)
{
string HostName = Dns.GetHostName();
lblHostName.Text=HostName;
IPHostEntry ipEntry = Dns.GetHostByName (HostName);
IPAddress [] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
if(ipEntry.AddressList.AddressFamily
listBox1.Items.Add("IP:"+ i.ToString() + " " + addr.ToString ());
}
}
//*********************************************
The code is fine, but when I run the program, I receive a list of various
IPs due to the interfaces that Ihave on this machines. I know what is the
address that I´m using to connect to the Internet, but how to do this using
code? It is always the last addres on the list?
Thanks in advance,
Rodrigo