Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone

I am developing an application where i need ipv4 IP address of the Computer.

I try this :

IPAddress LocalIp=
System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[0];

this return IPV6 IP.


There are more than one IP in Address List and system (e.g computer) use one IP at a time,
then how to detect which is the primary IP (means which IP is Currently using).

Please Help.
Posted
Updated 1-Mar-11 5:00am
v4

1 solution

You shouldn't just use one IP address, as there may be more than one ipv4 addresses.

List<IPAddress> localAddresses = new List<IPAddress>();

foreach (IPAddress ipAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
    if (ipAddress.AddressFamily == AddressFamily.InterNetwork) // filter out ipv4
    {
        localAddresses.Add(ipAddress);
    }
}
 
Share this answer
 
v4
Comments
sufi2008123 1-Mar-11 10:16am    
thank u very very much for u r reply...


There are more than one IP in Address List and system (e.g computer) use one IP at a time, then can u give me the idea how to detect which is the primary IP (means which IP is Currently using).
Dima Popov 1-Mar-11 15:18pm    
Well, it depends. Please, provide more information about your program.

Try this link:
http://stackoverflow.com/questions/359596/identifying-active-network-interface-in-net

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900