winforms 从连接的网卡获取本地IP

tp5buhyn  于 2023-10-23  发布在  其他
关注(0)|答案(4)|浏览(98)

我尝试获取我的本地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"; 
    }
mzaanser

mzaanser1#

我用这个:

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

os8fio9y2#

您可以使用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"; 
}
0yycz8jy

0yycz8jy3#

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

v440hwme4#

这将给予您正在寻找的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());
               }
           }
       }  
    }

相关问题