/*
* 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");
}
}
}
}
完整代码如上,运行效果如下: