⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getipofdn.cs

📁 根据域名获得IP地址列表.主要用到了IPHostEntry类以及GetHostByName方法
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;

namespace GetIPofDN
{

	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class GetIPofDN : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label lblDN;
		private System.Windows.Forms.TextBox txtDN;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.ListBox lstIPList;
		private System.Windows.Forms.Label lblIPList;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public GetIPofDN()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.lblDN = new System.Windows.Forms.Label();
			this.txtDN = new System.Windows.Forms.TextBox();
			this.btnOK = new System.Windows.Forms.Button();
			this.lstIPList = new System.Windows.Forms.ListBox();
			this.lblIPList = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// lblDN
			// 
			this.lblDN.Location = new System.Drawing.Point(24, 16);
			this.lblDN.Name = "lblDN";
			this.lblDN.Size = new System.Drawing.Size(128, 16);
			this.lblDN.TabIndex = 0;
			this.lblDN.Text = "Put into Domain Name:";
			// 
			// txtDN
			// 
			this.txtDN.Location = new System.Drawing.Point(24, 49);
			this.txtDN.Name = "txtDN";
			this.txtDN.Size = new System.Drawing.Size(248, 20);
			this.txtDN.TabIndex = 1;
			this.txtDN.Text = "";
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(280, 48);
			this.btnOK.Name = "btnOK";
			this.btnOK.TabIndex = 2;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// lstIPList
			// 
			this.lstIPList.Location = new System.Drawing.Point(24, 112);
			this.lstIPList.Name = "lstIPList";
			this.lstIPList.Size = new System.Drawing.Size(328, 173);
			this.lstIPList.TabIndex = 3;
			// 
			// lblIPList
			// 
			this.lblIPList.Location = new System.Drawing.Point(24, 88);
			this.lblIPList.Name = "lblIPList";
			this.lblIPList.Size = new System.Drawing.Size(192, 16);
			this.lblIPList.TabIndex = 4;
			// 
			// GetIPofDN
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(376, 301);
			this.Controls.Add(this.lblIPList);
			this.Controls.Add(this.lstIPList);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.txtDN);
			this.Controls.Add(this.lblDN);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "GetIPofDN";
			this.Text = "Get IP Address of Domain Name";
			this.ResumeLayout(false);

		}
		#endregion

		class ResolveDNS
		{
			IPAddress[] m_arrayIPs;
			public void Resolve(string s_host)
			{
				IPHostEntry hostInfo;
				try
				{
					hostInfo = Dns.GetHostByName(s_host);
					m_arrayIPs = hostInfo.AddressList;

				}
				catch(ArgumentNullException e)
				{
					MessageBox.Show("Source : " + e.Source + "	Message : " + e.Message);
				}
				catch(Exception e)
				{
					MessageBox.Show("Source : " + e.Source + "	Message : " + e.Message);
				}
				
			}

			public IPAddress this[int nIndex]
			{
				get
				{
					return m_arrayIPs[nIndex];
				}
			}

			public int IPLength
			{
				get
				{
					return m_arrayIPs.Length;
				}
			}

		}


		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new GetIPofDN());
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			lstIPList.Items.Clear();
			ResolveDNS resolver1 = new ResolveDNS();
			resolver1.Resolve(txtDN.Text);
			lblIPList.Text = "The IP address list of " + txtDN.Text + " are: ";
			int n = resolver1.IPLength;
			for( int i=0; i<n; i++ )
				lstIPList.Items.Add(resolver1[i]);
		}

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -