How to register a global hotkey for your application in C#

How to register a global hotkey for your application in C#

Register a hotkey for your application that can be triggered even if your form is minimized and does not have focus. More »

How to download a file in C# (progressbar and download speed)

How to download a file in C# (progressbar and download speed)

Learn how to download files in C# while displaying the percentage and the download speed. More »

UDP hole punching implementation in C#

UDP hole punching implementation in C#

Learn how to implement UDP hole punching so you can make your clients life a lot easier by not forcing them to open ports on their end. More »

 

How to get your IP in C#

There are two ways of getting your IP address in C#. One of them is by using the methods provided by the .NET framework and the other one by using a third party api.

What is the difference you might ask. The answer is fairly simple. There are two kinds of IP’s, internal IP’s and external IP’s. Sadly the .NET framework methods are not able to retrieve the external IP if the computer is not directly connected to the internet (no router or any other similar devices). Since this limitation exists there is only one option if you wish to get the external IP, and this is by using an online service.

I’ve constructed two methods below. One uses only the .NET framework methods but is not 100% accurate when trying to retrieve the external IP (requires the system to be directly connected to the internet). The second method uses an online service which should have fairly accurate results as long as the service is online and the computer is not behind a proxy.

Method 1
LINQ approach

Non-LINQ approach (for .NET framework versions before 3.5)

Method 2

Share on FacebookTweet about this on TwitterShare on Google+Share on StumbleUponShare on LinkedInShare on RedditPin on PinterestShare on TumblrDigg thisPrint this pageEmail this to someone

3 Responses to How to get your IP in C#

  1. James Newlyn says:

    If your Router display external WAN IP you can query the router itself.

  2. Max says:

    Thank you so much. I’ve been trying for a week to figure out how to do this… I originally programmed something similar in python, but was unable to figure out how to do it in C#.

    Once again, thanks!

    • Jeetendra says:

Leave a Reply

Your email address will not be published. Required fields are marked *