【问题标题】:get Local IP from connected networkcard从连接的网卡获取本地 IP
【发布时间】:2017-11-17 19:21:50
【问题描述】:

我尝试获取我的本地 IP,下面的代码向您展示了我是如何做到的。
这个问题是当我连接到 wifi 并且我的以太网卡没有连接时
它给了我一个 169.xxx.xxx.xxx 地址。
我如何检查以查看已连接的网卡,并获取该 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"; 
    }

【问题讨论】:

    标签: c# winforms ip


    【解决方案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);
        }
      }
    } 
    

    【讨论】:

      【解决方案2】:

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

      这段代码sn-p应该会给你你感兴趣的结果:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-03-20
            • 1970-01-01
            • 2012-02-04
            • 2015-08-13
            • 2014-09-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多