How To Find IP Address In C# Visual Studio


How To Find IP Address In C# Visual Studio

This sample code uses the system.NET namespace to get the hostname and then gets the IP address for that hostname Find IP Address. C#

Get IP Address Using C# Code. Procedure. Step

 1: Start a new Console project in your Visual Studio. 

2: Add a namespace in your project as in the following namespace (Using System.Net)

3: Before fetching the IP Address we need to know whose IP Address we want.

4: We are now ready to get the IP address

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Net; //Include this namespace  
   
   
namespace IpProto  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {   
            string hostName = Dns.GetHostName(); // Retrive the Name of HOST  
            Console.WriteLine(hostName);  
           // Get the IP  
            string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();  
            Console.WriteLine("My IP Address is :"+myIP);  
            Console.ReadKey();  
        }  
    }  
}

Stage 1: Start another Console venture in your Visual Studio.

Stage 2: Add a namespace in your venture as in the accompanying:

Utilizing System.Net;

Stage 3: Before getting the IP Address we have to realize whose IP Address we truly need. It’s very comprehended that we need our own PC. In any case, that must be indicated in my code since PCs are stupid.

Along these lines, we can get the machine Name (or Host Name) by the GetHostName() Method. What’s more, this is inside the Dns class.

Stage 4: We are presently prepared to get the IP address of the Host.

For this we have to utilize the GetHostByName() technique followed by AddressList exhibit (first Index).

What’s more, in GetHostByName’s contention we “have name”.

Have any Question or Comment?

Leave a Reply

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

Categories

%d bloggers like this: