views:

5296

answers:

5

I need to get the actual local network IP address of the computer (e.g. 192.168.0.220) from my program using C# and .NET 3.5. I can't just use 127.0.0.1 in this case.

What's the best way to do this?

+10  A: 

Link It says there, add System.net, and using the following

    //To get the local IP address 
string sHostName = Dns.GetHostName (); 
IPHostEntry ipE = Dns.GetHostByName (sHostName); 
IPAddress [] IpA = ipE.AddressList; 
for (int i = 0; i < IpA.Length; i++) 
{ 
    Console.WriteLine ("IP Address {0}: {1} ", i, IpA[i].ToString ()); 
}
PostMan
Which IP Address in the array that you get back is the right one?
EBGreen
GetHostByName showing as deprecated. wound up using:IPAddress[] ipAddress = Dns.GetHostAddresses (strHostName);accomplishes same thing.
scottmarlowe
Also, Hungarian notation is almost dead at this point. Use string hostName.
JC
+8  A: 

As a machine can have multiple ip addresses, the correct way to figure out your ip address that you're going to be using to route to the general internet is to open a socket to a host on the internet, then inspect the socket connection to see what the local address that is being used in that connection is.

By inspecting the socket connection, you will be able to take into account weird routing tables, multiple ip addresses and whacky hostnames. The trick with the hostname above can work, but I wouldn't consider it entirely reliable.

Jerub
+11  A: 

If you are looking for the sort of information that the command line utility, ipconfig, can provide, you should probably be using the System.Net.NetworkInformation namespace.

This sample code will enumerate all of the network interfaces and dump the addresses known for each adapter.

using System;
using System.Net;
using System.Net.NetworkInformation;

class Program
{
    static void Main(string[] args)
    {
     foreach ( NetworkInterface netif in NetworkInterface.GetAllNetworkInterfaces() )
     {
      Console.WriteLine("Network Interface: {0}", netif.Name);
      IPInterfaceProperties properties = netif.GetIPProperties();
      foreach ( IPAddress dns in properties.DnsAddresses )
       Console.WriteLine("\tDNS: {0}", dns);
      foreach ( IPAddressInformation anycast in properties.AnycastAddresses )
       Console.WriteLine("\tAnyCast: {0}", anycast.Address);
      foreach ( IPAddressInformation multicast in properties.MulticastAddresses )
       Console.WriteLine("\tMultiCast: {0}", multicast.Address);
      foreach ( IPAddressInformation unicast in properties.UnicastAddresses )
       Console.WriteLine("\tUniCast: {0}", unicast.Address);
     }
    }
}

You are probably most interested in the UnicastAddresses.

GBegen
+4  A: 

Using Dns requires that your computer be registered with the local DNS server, which is not necessarily true if you're on a intranet, and even less likely if you're at home with an ISP. It also requires a network roundtrip -- all to find out info about your own computer.

The proper way:

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach(NetworkInformation adapter in  nics)
{
    foreach(var x in adapterProperties.UnicastAddresses)
    {
    if (x.Address.AddressFamily == AddressFamily.InterNetwork)
             Console.WriteLine(" IPAddress ........ : {0:x}", x.Address.ToString());
    }
}
James Curran
A: 

Hi,

This always returns 127.0.0.1 for me.

Any other solution ?

Cheers,

Mrunal Buch
You can perform additional check with IPAddress.IsLoopback(...).If non-loopback is present - return it
Vadmyst