Usage of the class library Net.Dns

Basic usage:

using System;
using System.Net;
using Bdev.Net.Dns;

namespace DNS_check_tool
{
    class Program
    {
        static void Main(string[] args)
        {
            IPAddress dnsServer = IPAddress.Parse("8.8.8.8");
            string domain = "example.com";

            Request request = new Request();
            request.AddQuestion(new Question(domain, DnsType.NS, DnsClass.IN)); 

            Response response = Resolver.Lookup(request, dnsServer);

            if (response == null)
            {
                Console.Write("no answer \n");
                return;
            }
            if (response.AuthoritativeAnswer)
            {
                Console.Write("Authorative Answer\n");
            }
            else
            {
                Console.Write("Non-Authorative Answer\n");
            }

            foreach (Answer answer in response.Answers)
            {
                Console.Write(answer.Type.ToString() + " (" + answer.Domain.ToString() + ") " + answer.Record.ToString() + "\n");
            }
            foreach (NameServer nameServer in response.NameServers)
            {
                Console.Write(nameServer.Type.ToString() + " (" + nameServer.Domain.ToString() + ") " + nameServer.Record.ToString() + "\n");
            }
            foreach (AdditionalRecord additionalRecord in response.AdditionalRecords)
            {
                Console.Write(additionalRecord.Type.ToString() + " (" + additionalRecord.Domain.ToString() + ") " + additionalRecord.Record.ToString() + "\n");
            }

        }
    }
}

To get the DNS servers you can use an instance of the DnsProvider class to get them using the SystemDnsServers method.
Example from the sample application:
foreach (IPAddress ip in DnsProvider.SystemDnsServers())
{
    comboBox1.Items.Add(ip.ToString());
}
Additionally we have created a small read only variable (DefaultDnsServers) that you can get some pre-defined DNS servers that you should use as a fallback.

Additionally you can look at the included sample application from the repository for ideas on how the library works

Last edited Aug 14, 2011 at 1:55 PM by TuxyQ, version 7

Comments

No comments yet.