How to get local IP address of Windows system?

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

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

2条回答
  •  猫巷女王i
    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.

提交回复
热议问题