How to get WSL2 IP address from C# program?

Viewed 342

I'm trying to get WSL'2 IP address, in windows and ifconfig it is displaying as 172.30.78.85. How can I get it from C# code? I tried this:

  public static string GetLocalIPAddress()
  {
     var addresses = Dns.GetHostAddresses(Dns.GetHostName());
     foreach (var ip in addresses)
     {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
           return ip.ToString();
        }
     }
     throw new Exception("No network adapters with an IPv4 address in the system!");
  }

But it returns 127.0.0.1. Program runs inside WSL.

0 Answers
Related