c#编写的局域网IP查询工具

/*

 * Created by SharpDevelop.

 * User: Administrator

 * Date: 2008-6-24

 * Time: 10:08

 * 

 * To change this template use Tools | Options | Coding | Edit Standard Headers.

 */



using System;

using System.Collections.Generic;

using System.Drawing;

using System.Windows.Forms;

using System.Net;

using System.IO;

using System.Threading;

using System.Net.NetworkInformation;



namespace workgroupscanle

{

	/// <summary>

	/// Description of MainForm.

	/// </summary>

	public partial class MainForm

	{

		[STAThread]

		public static void Main(string[] args)

		{

			Application.EnableVisualStyles();

			Application.SetCompatibleTextRenderingDefault(false);

			Application.Run(new MainForm());

		}

		

		public MainForm()

		{

			//

			// The InitializeComponent() call is required for Windows Forms designer support.

			//

			InitializeComponent();

			

			//

			// TODO: Add constructor code after the InitializeComponent() call.

			//

		}

		

		void Button1Click(object sender, System.EventArgs e)

		{

			IPHostEntry myHost = new IPHostEntry();

			try

			{

 				myHost = Dns.GetHostEntry(Dns.GetHostName());

				textBox1.Text = myHost.HostName.ToString();

				for(int i=0; i<myHost.AddressList.Length;i++)

				{

 					richTextBox1.AppendText("本地主机IP地址->"+myHost.AddressList[i].ToString()+"/r");

				}

			}

            catch(Exception error)

			{

				MessageBox.Show(error.Message);

            }



		}

		

		void Button2Click(object sender, System.EventArgs e)

		{

			IPHostEntry myDnsToIp = new IPHostEntry();

			myDnsToIp = Dns.GetHostEntry(textBox2.Text);

			for(int i=0;i<myDnsToIp.AddressList.Length;i++)

			{

				richTextBox1.AppendText(textBox2.Text+"的IP为"+myDnsToIp.AddressList[i].ToString()+"/r");

			}

			for(int j=0;j<myDnsToIp.Aliases.Length;j++)

			{

				richTextBox1.AppendText(myDnsToIp.Aliases[j].ToString()+"/r");

			}

		}

		

		void Button3Click(object sender, System.EventArgs e)

		{

			ScanTarget();

		}

		private void ScanTarget()

		{

			string strIPAddress=numericUpDown1.Text+"."+numericUpDown2.Text+"."+numericUpDown3.Text+".";

			int nStart = Int32.Parse(numericUpDown4.Text);

			int nEnd = Int32.Parse(numericUpDown5.Text);

			try

			{

				for(int i=nStart;i<=nEnd;i++)

				{

					ProgressBar1.Visible = true;

					ProgressBar1.Value=(i-nStart)*100/(Math.Abs(nEnd-nStart));

					Ping myPing = new Ping();

					myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);

					string strScanIPAdd = strIPAddress+i.ToString();

					myPing.SendAsync(strScanIPAdd,1000,null);

				}

			}

			catch

			{

				

			}

			ProgressBar1.Visible = false;

		}

			private void _myPing_PingCompleted(object sender, PingCompletedEventArgs e)

			{

				if(e.Reply.Status==IPStatus.Success)

				{

					richTextBox1.AppendText(e.Reply.Address.ToString()+"的计算机名为"+Dns.GetHostByAddress(IPAddress.Parse(e.Reply.Address.ToString())).HostName+"/r");

				}

			}

	}

}

完整代码如上,运行效果如下:

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值