IP Utils

using System.Net; using System.Net.Sockets; public static class IPUtils { /// <summary> /// Retrieve the local IP address of the current machine /// </summary> /// <returns>The local IP address (for example, 192.168.1.xxx</returns> public static string GetLocalIPAddress() { foreach (IPAddress ip in Dns.GetHostEntry(Dns.GetHostName()).AddressList) if (ip.AddressFamily == AddressFamily.InterNetwork) return ip.ToString(); return ""; } static string LastPublicIPAddress; /// <summary> /// Retrieve the public IP address of the current machine /// </summary> /// <returns>The public IP address</returns> public static string GetPublicIPAddress() { if (LastPublicIPAddress != null) return LastPublicIPAddress; using (var wc = new WebClient()) LastPublicIPAddress = wc.DownloadString("http://checkip.dyndns.org/").Split(':')[1].Trim().Split('<')[0]; return LastPublicIPAddress; } }
This class allows you to retrieve your local and your public IP an easy way. Note that you need internet connection for getting your public IP

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.