get Local IP from connected networkcard

时间:2017-06-15 10:26:46

标签: c# winforms ip

I try to get my local ip, the code below shows you how i do that.
The problem with this is when i'm connectet to wifi and my ethernet-card is not connected
it gives me a 169.xxx.xxx.xxx adress.
How can i make a check to see wich networkcard 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"; 
    }

4 个答案:

答案 0 :(得分:1)

我用这个:

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

答案 1 :(得分:0)

您可以使用NetworkInterface类来查看是否存在确实已连接的网络接口。请在此处查看相关文档:https://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface(v=vs.110).aspx

此代码段应该为您提供您感兴趣的结果:

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"; 
}

答案 2 :(得分:0)

这对我有用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();
        }
     }

答案 3 :(得分:0)

这将提供您正在寻找的IP地址

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.122.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>