我试图在Windows和ifconfig中获取WSL'2 IP地址,它显示为172.30.78.85
。如何从C#代码中获取?我尝试过:
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!");
}
但是它返回127.0.0.1
。程序在WSL中运行。