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

Posted on 2010-05-16 14:50  严武  阅读(273)  评论(0编辑  收藏  举报

 

/*

 * 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");

    }

   }

 }

}

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

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/szj3714/archive/2008/06/24/2582808.aspx

Copyright © 2024 严武
Powered by .NET 8.0 on Kubernetes