Reputation: 3156
I have a list of computers by name. I'd like to see if the computers on the network are connected by using the System.Net.Ping
and System.Net.PingReply
. The list of computers may become stale from time to time. If the host is not found I get an exception. Is there a way that I can check to see if there is a record of the host without experiencing an exception? I thought I might be able to use Dns.GetHostEntry(c.Name)
but this also returns an exception if the host is not in DNS.
Upvotes: 1
Views: 1533
Reputation: 2940
If you're unable to use a try/catch, you could use SimpleDNS (Nuget as well) which only throws exceptions on connection issues to the chosen DNS server.
var dnsServer = IPAddress.Parse("8.8.8.8");
var result = Query.Simple(new SimpleDnsPacket(new Question("lalalalalaldasl.com", QType.A), dnsServer)); //Failing Query
The return DNS packet object will be null if query was invalid:
and rather than checking for null
, you could query the Response
property found in the Header:
//Continued on from above....
if (result.Header.Parameters.Response != ResponseCode.Ok)
{
//Something here!
}
Upvotes: 2