using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Threading;
namespace WindowLanSearch
{
/// <summary>
/// Form 的摘要说明。
/// </summary>
public class Form : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox;
private System.Windows.Forms.Button button;
private string[,] LanHost;
private System.Windows.Forms.ProgressBar progressBarSearch;
private Thread[] thread;
private System.Windows.Forms.ListView listView;
private System.Windows.Forms.ColumnHeader columnHeader;
private System.Windows.Forms.ColumnHeader columnHeader;
private string str;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
InitLanHost();
progressBarSearch.Maximum = ;
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 数组初始化
/// </summary>
private void InitLanHost()
{
LanHost = new string[,];
for (int i=0;i<;i++)
{
LanHost[i,0] = "";
LanHost[i,] = "";
}
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox = new System.Windows.Forms.TextBox();
this.button = new System.Windows.Forms.Button();
this.progressBarSearch = new System.Windows.Forms.ProgressBar();
this.listView = new System.Windows.Forms.ListView();
this.columnHeader = new System.Windows.Forms.ColumnHeader();
this.columnHeader = new System.Windows.Forms.ColumnHeader();
this.SuspendLayout();
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(, 0);
this.textBox.Multiline = true;
this.textBox.Name = "textBox";
this.textBox.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.textBox.Size = new System.Drawing.Size(76, 96);
this.textBox.TabIndex = 0;
this.textBox.Text = "";
//
// button
//
this.button.Location = new System.Drawing.Point(6, 0);
this.button.Name = "button";
this.button.TabIndex = ;
this.button.Text = "开始搜索";
this.button.Click += new System.EventHandler(this.button_Click);
//
// progressBarSearch
//
this.progressBarSearch.Location = new System.Drawing.Point(, 60);
this.progressBarSearch.Name = "progressBarSearch";
this.progressBarSearch.Size = new System.Drawing.Size(90, );
this.progressBarSearch.TabIndex = ;
//
// listView
//
this.listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader,
this.columnHeader});
this.listView.Location = new System.Drawing.Point(8, 0);
this.listView.Name = "listView";
this.listView.Size = new System.Drawing.Size(8, 88);
this.listView.TabIndex = ;
//
// columnHeader
//
this.columnHeader.Text = "dddd";
//
// columnHeader
//
this.columnHeader.Text = "sssss";
//
// Form
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, );
this.ClientSize = new System.Drawing.Size(, );
this.Controls.Add(this.listView);
this.Controls.Add(this.progressBarSearch);
this.Controls.Add(this.button);
this.Controls.Add(this.textBox);
this.Name = "Form";
this.Text = "Form";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form());
}
private void button_Click(object sender, System.EventArgs e)
{
LanSearch();
}
/// <summary>
/// 局域网搜索事件
/// </summary>
private void LanSearch()
{
thread = new Thread[];
ThreadStart threadMethod;
Thread threadProgress = new Thread(new ThreadStart(progressSearch));
threadProgress.Start();
string localhost = (Dns.GetHostByName(Dns.GetHostName())).AddressList[0].ToString(); //本地主机IP地址
str = localhost.Substring(0,localhost.LastIndexOf("."));
for (int i=0;i<;i++) //建立个线程扫描IP
{
threadMethod = new ThreadStart(LanSearchThreadMethod);
thread[i] = new Thread(threadMethod);
thread[i].Name = i.ToString();
thread[i].Start();
if (!thread[i].Join(00)) //Thread.Join(00)不知道这处这么用对不对,感觉没什么效果一样
{
thread[i].Abort();
}
}
GetLanHost();
listLanHost();
}
/// <summary>
/// 多线程搜索方法
/// </summary>
private void LanSearchThreadMethod()
{
int Currently_i = Convert.ToUInt6(Thread.CurrentThread.Name); //当前进程名称
IPAddress ScanIP = IPAddress.Parse( str + "."+Convert.ToString(Currently_i +)); //获得扫描IP地址
IPHostEntry ScanHost = null;
ScanHost = Dns.GetHostByAddress(ScanIP); //获得扫描IP地址主机信息
if (ScanHost != null)
{
LanHost[Currently_i,0] = ScanIP.ToString();
LanHost[Currently_i,] = ScanHost.HostName;
}
//progressBarSearch.Value = progressBarSearch.Value +;
}
/// <summary>
/// 文本框显示主机名与IP列表
/// </summary>
private void GetLanHost()
{
for (int i=0;i<;i++)
if ( LanHost[i,0] !="")
{
textBox.Text =textBox.Text + LanHost[i,] +":" +LanHost[i,0] + "\r\n";
}
}
/// <summary>
/// listview 显示搜索主机
/// </summary>
private void listLanHost()
{
listView.View = View.List;
ListViewItem aa ;
for (int i=0;i<;i++)
{
if ( LanHost[i,0] !="")
{
aa= new ListViewItem();
aa.Text = LanHost[i,];
aa.Tag = LanHost[i,0];
listView.Items.Add(aa);
}
}
}
/// <summary>
/// 进度条处理线程
/// </summary>
private void progressSearch()
{
//label.Text = "进度条只是时间估计,不是真实搜索进度!";
progressBarSearch.Value = 0;
for (int i=0;i<;i++)
{
progressBarSearch.Value = progressBarSearch.Value + ;
Thread.Sleep(00);
}
}
}
}
遗憾之处:因搜索较慢,没有实现真实的搜索进度。
不懂之处:实现文字提示时,当在鼠标事件首尾插入
private void button_Click(object sender, System.EventArgs e)
{
lab.Text = “开始搜索”; //新插入
LanSearch();
lab.Text = “结束搜索”; //新插入
}
文本提示时,在lab上始终不能及时显示,而是等所有线程结束后才显示“结束搜索“。
初学winForm编程,太多的要学了。
望高手指点一二








