Friday, March 20, 2015

Using DNS Class

...
using System.Windows.Forms;
using System.Net; //Added
using System.Net.Sockets; //Added



namespace UsingDNSClass
{
    public partial class Form1 : Form
    {
        //To read more about dns class:
        //://msdn.microsoft.com/en-us/library/system.net.dns(v=vs.110).aspx
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLocalComputer_Click(object sender, EventArgs e)
        {
            string hostname = Dns.GetHostName();
            richTextBox1.Text =
                "Local computer name: " + hostname;

            //get the IP address of this computer
            IPAddress[] ipAddresses = Dns.GetHostAddresses(hostname);

            //display
            richTextBox1.AppendText("\n\nIP Address: \n");
            foreach (IPAddress address in ipAddresses)
                richTextBox1.AppendText(address + "\n");
        }

        //://msdn.microsoft.com/en-us/library/system.net.dns.endgethostentry(v=vs.110).aspx
        private void btnGetIPAddresses_Click(object sender, EventArgs e)
        {
            try
            {
                //get the hostentry
                string hostName = txtDomainName.Text;
                IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
                IPAddress[] ipaddresses = hostEntry.AddressList;

                //display
                richTextBox1.AppendText("\n\nIP Address: \n");
                foreach (IPAddress address in ipaddresses)
                    richTextBox1.AppendText(address + "\n");

            }
            //s://msdn.microsoft.com/en-us/library/system.net.iphostentry.addresslist(v=vs.110).aspx
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }

        private void btnGetIPAddressesAsynchronously_Click(object sender, EventArgs e)
        {
            //://msdn.microsoft.com/en-us/library/hh194304(v=vs.110).aspxtry
            try
            {
                //get the hostentry

                //display

            }
            //s://msdn.microsoft.com/en-us/library/system.net.iphostentry.addresslist(v=vs.110).aspx
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }

        }
        //        [HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]
        //public static Task<IPHostEntry> GetHostEntryAsync(
        //    IPAddress address
        //)
    }
}

No comments:

Post a Comment