Answers for "how to get ip address in c#"

C#
10

how to get ip address in c#

public static string GetLocalIPAddress()
{
    var host = Dns.GetHostEntry(Dns.GetHostName());
    foreach (var ip in host.AddressList)
    {
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            return ip.ToString();
        }
    }
    throw new Exception("No network adapters with an IPv4 address in the system!");
}
Posted by: Guest on June-25-2020
0

c# get ip address

var httpClient = new HttpClient();
var ip = await httpClient.GetStringAsync("https://api.ipify.org");
Console.WriteLine($"My public IP address is: {ip}");
Posted by: Guest on January-06-2021

C# Answers by Framework

Browse Popular Code Answers by Language