How to Get the IP address of a domain name in C# ?

There are times when you want to resolve the domain name to an IP address programmatically from your C# Application . In this case , you can use the Dns.GetHostAddresses method defined in the System.Net namespace.

The Dns.GetHostAddresses returns an array of type System.Net.IPAddress which contains the IP addresses for the host that is specified by the hostname parameter.

How to Get the IP address of a domain name in C# ?

Below is a code snippet demonstrating the usage of Dns.GetHostAddresses to get the IP address of the domain name.

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace MobileOSGeekApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var Address = Dns.GetHostAddresses("www.developerpublish.com").FirstOrDefault();
            Console.WriteLine(Address.ToString());          
            Console.ReadLine();
        }
       
    }
    
}

 

image

Leave A Reply

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

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...