user7232036
user7232036

Reputation:

get Local IP from connected network card

I try to get my local ip, the code below shows you how I do that.
The problem with this is when I'm connected to wifi and my ethernet-card is not connected it gives me a 169.xxx.xxx.xxx address.
How can I make a check to see which network card is connected, and get that ip?

private string GetLocalIP()
    {
        IPHostEntry host;
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
                return ip.ToString();
        }
        return "127.0.0.1"; 
    }

Upvotes: 0

Views: 1043

Answers (4)

Rohit Poudel
Rohit Poudel

Reputation: 1889

this will give the IP addresses you are looking for

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());
               }
           }
       }  
    }

Upvotes: 0

user7232036
user7232036

Reputation:

this works for me https://stackoverflow.com/a/27376368/7232036

 private string localIP()
    {
        using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
        {
            socket.Connect("8.8.8.8", 65530);
            IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
            return endPoint.Address.ToString();
        }
     }

Upvotes: 0

KBO
KBO

Reputation: 675

I use this:

List<IPAddress>    addresses = new List<IPAddress>();
NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface networkItf in networkInterfaces)
{
  // check whether turned on
  if (networkItf.OperationalStatus == OperationalStatus.Up)
  {
    // read the IP configuration for each network 
    IPInterfaceProperties properties = networkItf.GetIPProperties();

    // each network interface may have multiple IP addresses 
    foreach (IPAddressInformation address in properties.UnicastAddresses)
    {
      // filter only IPv4 addresses, if required...
      if (address.Address.AddressFamily != AddressFamily.InterNetwork)
        continue;

      // ignore loopback addresses (e.g., 127.0.0.1) 
      if (IPAddress.IsLoopback(address.Address))
        continue;

      // add result
      addresses.Add(address.Address);
    }
  }
} 

Upvotes: 1

Sebastian Brandes
Sebastian Brandes

Reputation: 746

You can use the NetworkInterface class to see if there is a network interface that is indeed connected. Take a look at the relevant documentation here: https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface(v=vs.110).aspx

This code snippet should give you the result you are interested in:

private string GetLocalIP()
{
    var isConnected = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();

    if (isConnected)
    {
        IPHostEntry host;
        host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (IPAddress ip in host.AddressList)
        {
            if (ip.AddressFamily == AddressFamily.InterNetwork)
                return ip.ToString();
        }
    }

    return "127.0.0.1"; 
}

Upvotes: 0

Related Questions