C#DNS域名解析工具(DnsLookup)

C#DNS域名解析工具(DnsLookup)

DNS域名解析工具:DnsLookup

输入域名后点击Resolve按钮即可。

主要实现代码如下:

复制代码
 private void btnResolve_Click ( object sender, EventArgs e ) {
            lstIPs.Items.Clear ( );  //首先把结果里的ListBox清空  
            try {
                IPHostEntry iphost = Dns.GetHostEntry ( txtInput.Text );   //解析主机 
                foreach ( IPAddress  ip in iphost.AddressList ) {   //iphost.AddressList这里面是从主机上获取的所有的IP,所以要用Foreach遍历出来
                    string ipaddress = ip.AddressFamily.ToString ( );
                    lstIPs.Items.Add ( ipaddress );
                    lstIPs.Items.Add ("  "+ip.ToString ());
                }
                txtHostName.Text  = iphost.HostName;   //主机名 
            }
            catch ( Exception ex) {
                MessageBox.Show ( "Unable to process the request because" +
                    " the following problem occurred:\n" +
                    ex.Message, "Exception occurred" );  //这里是异常处理  例如网络连接或主机不能解析等其它问题就显示出来给用户
            }
        }

        private void txtInput_KeyPress ( object sender, KeyPressEventArgs e ) {
            if ( e.KeyChar==(char)13 ) {  //大家在敲完后都习惯性回车确定  这里就是在Input里输入完后直接回车,相当于点击了Resolve这个按钮
                btnResolve_Click ( this, null );
            }
        }
复制代码

你可能感兴趣的:(C#DNS域名解析工具(DnsLookup))