Get Ip Address Using C#


In general, we can find our IP address by CMD command in other words ipconfig . And, if you execute this on your Administrator Command Prompt then you will get the basic Network details like Host Name, IP Address and Gateway.

Step 1: Start a new Console project in your Visual Studio.
Step 2: Add a namespace in your project as in the following:

Using System.Net;

Step 3: Before fetching the IP Address we need to know whose IP Address we really want. It’s quite understood that we want our own PC. But, that must be specified in my code because computers are dumb.

So, we can fetch the machine Name (or Host Name) by the GetHostName() Method. And this is inside the Dns class.

Step 4: We are now ready to get the IP address of the Host.

For this we need to use the GetHostByName() method followed by AddressList array (first Index).

And, in GetHostByName’s argument we “host name”.



Please enter your comment!
Please enter your name here