german-flag  british-flag    Sitemap   |   Imprint
logo

Advertisement
Get IP Address - CSharp
This CSharp (C#) code snipped shows how to get the external IP address (IPv4) of the machine. To achieve this task, we get all ip addresses of the machine and compare them against the local address ranges 10.0.0.0/24, 172.16.0.0/16, 192.168.0.0/16 and 169.254.0.0/16.

private void ListIPAddresses()
{
    IPHostEntry myIPHostEntry = Dns.GetHostEntry(Dns.GetHostName());

    foreach (IPAddress myIPAddress in myIPHostEntry.AddressList)
    {
        Console.WriteLine(myIPAddress.ToString());    
    }
}

stickler.de © 2000 - 2010 Björn Stickler