Updated on Kisan Patel
Here we will show you a simple and easy way to get public or external IP address in C#.
Here we will use freegeoip.net public HTTP API that provides a public HTTP API for software developers to search the geolocation of IP addresses.
public static string getExternalIp() { try { string externalIP; externalIP = (new WebClient()).DownloadString("http://freegeoip.net/json/"); externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")) .Matches(externalIP)[0].ToString(); return externalIP; } catch { return null; } }
Example
class Program { static void Main(string[] args) { string stringIPAddress = getExternalIp(); Console.WriteLine("Your Public IP Address is: " + stringIPAddress); Console.ReadKey(); } }
Output of the above program…