How to get local IP address of Windows system?

前端 未结 2 1575
攒了一身酷
攒了一身酷 2021-01-16 18:33

Is there any code or method to get the IP addresses of the local system?

相关标签:
2条回答
  • 2021-01-16 19:16

    If you are in C#, you could use .NET:

    using System;
    using System.Net;
    
    public static string GetLocalIP() {
      var hosts = Dns.GetHostEntry(Dns.GetHostName());
      foreach (var ipEntry in host.AddressList)
      {
        if (ipEntry.AddressFamily == AddressFamily.InterNetwork)
        {
          return ip.ToString();
        }
      }
    }
    

    You can later add a throw new Exception at the end, it will show up if you have no IPv4 adapters installed.

    0 讨论(0)
  • 2021-01-16 19:40

    To enumerate local IP addresses, use the Win32 API GetAdaptersInfo() (supports IPv4 only) or GetAdaptersAddresses() (supports IPv4 and IPv6) function. C/C++ examples are included in their documentation.

    0 讨论(0)
提交回复
热议问题