2

In Code i want to validate a domain name.

For example : " DomainName.com".

How can i do that in C#.

I worked on MSDN Solution. (Second Solution).

But "PingCompletedCallback" is not getting executed.

Thanx

Preeti
  • 1,386
  • 8
  • 57
  • 112
  • That won't validate the domain name - you'd be better off doing a DNS query instead – Rowland Shaw May 13 '10 at 09:39
  • @Rowland yup, pinging a domain to validate it is totally useless. – Pekka May 13 '10 at 09:43
  • Can you clarify what you want to do exactly. Are you wanting to do a name lookup to check whether a domain name has been registered or are you wanting to test whether a particular host is up and responsive? Your tags seem to conflict with what you're asking. – Chris W May 13 '10 at 10:15

4 Answers4

2

I don't think pinging a domain name will tell you anything relevant in a reliable way.

  • A domain name can be registered but not connected to a server. Ping requests will fail even though the domain is registered.

  • A server can be fully operational but be configured not to respond to ping requests. Ping requests will fail even if the domain is registered and running on a server.

What do you want to do - find out whether a domain is registered, whether it's a valid domain name, or whether it's a working website / mail server ...?

For the first two, I would recommend using a whois service. See for example these C# related questions:

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
2

There's a class for that;

MSDN Article on System.Net.NetworkInformation.Ping

GeekPedia article on asynchronous ping

Sanarothe
  • 192
  • 2
  • 12
2

Using the System.Net.NetworkInformation.Ping class,

using System.Net.NetworkInformation;

Ping sender = new Ping();
PingReply reply = sender.Send ("www.example.com");

if (reply.Status == IPStatus.Success)
{
  Console.WriteLine("Ping successful.");
}

It's untested, but that's the general idea.

rbuch
  • 70
  • 1
  • 8
  • I am getting : An unhandled exception of type 'System.Net.NetworkInformation.PingException' occurred in System.dll Additional information: An exception occurred during a Ping request. – Preeti May 13 '10 at 09:51
1

You could use the Ping class - here's all the info you need on MSDN

Chris W
  • 3,304
  • 3
  • 26
  • 28