您的位置:首页 > 编程语言 > C#

c# 获得局域网主机列表实例

2005-01-13 08:46 447 查看
1// 局域网主机搜索
2// 日期:2005.01.12
3// 作者:nanfansky
4// 参考:http://blog.aspcool.com/jiezhi
5
6using System;
7using System.Drawing;
8using System.Collections;
9using System.ComponentModel;
10using System.Windows.Forms;
11using System.Data;
12using System.Net;
13using System.Threading;
14
15namespace WindowLanSearch
16{
17 /// <summary>
18 /// Form1 的摘要说明。
19 /// </summary>
20 public class Form1 : System.Windows.Forms.Form
21 {
22  private System.Windows.Forms.TextBox textBox1;
23  private System.Windows.Forms.Button button1;
24  private string[,] LanHost;
25  private System.Windows.Forms.ProgressBar progressBarSearch;
26  private Thread[]  thread;
27  private System.Windows.Forms.ListView listView1;
28  private System.Windows.Forms.ColumnHeader columnHeader1;
29  private System.Windows.Forms.ColumnHeader columnHeader2;
30  private string str;
31  /// <summary>
32  /// 必需的设计器变量。
33  /// </summary>
34  private System.ComponentModel.Container components = null;
35
36  public Form1()
37  {
38   //
39   // Windows 窗体设计器支持所必需的
40   //
41   InitializeComponent();
42   InitLanHost();
43   progressBarSearch.Maximum = 255;
44
45   //
46   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
47   //
48  }
49
50  /// <summary>
51  /// 数组初始化
52  /// </summary>
53  private void InitLanHost()
54  {
55   LanHost = new string[255,2];
56   for (int i=0;i<255;i++)
57   {
58    LanHost[i,0] = "";
59    LanHost[i,1] = "";
60   }
61  }
62
63  /// <summary>
64  /// 清理所有正在使用的资源。
65  /// </summary>
66  protected override void Dispose( bool disposing )
67  {
68   if( disposing )
69   {
70    if (components != null)
71    {
72     components.Dispose();
73    }
74   }
75   base.Dispose( disposing );
76  }
77  #region Windows 窗体设计器生成的代码   ...
78
79  /// <summary>
80  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
81  /// 此方法的内容。
82  /// </summary>
83  private void InitializeComponent()
84  {
85   this.textBox1 = new System.Windows.Forms.TextBox();
86   this.button1 = new System.Windows.Forms.Button();
87   this.progressBarSearch = new System.Windows.Forms.ProgressBar();
88   this.listView1 = new System.Windows.Forms.ListView();
89   this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
90   this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
91   this.SuspendLayout();
92   //
93   // textBox1
94   //
95   this.textBox1.Location = new System.Drawing.Point(24, 40);
96   this.textBox1.Multiline = true;
97   this.textBox1.Name = "textBox1";
98   this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
99   this.textBox1.Size = new System.Drawing.Size(176, 296);
100   this.textBox1.TabIndex = 0;
101   this.textBox1.Text = "";
102   //
103   // button1
104   //
105   this.button1.Location = new System.Drawing.Point(456, 40);
106   this.button1.Name = "button1";
107   this.button1.TabIndex = 1;
108   this.button1.Text = "开始搜索";
109   this.button1.Click += new System.EventHandler(this.button1_Click);
110   //
111   // progressBarSearch
112   //
113   this.progressBarSearch.Location = new System.Drawing.Point(32, 360);
114   this.progressBarSearch.Name = "progressBarSearch";
115   this.progressBarSearch.Size = new System.Drawing.Size(490, 24);
116   this.progressBarSearch.TabIndex = 2;
117   //
118   // listView1
119   //
120   this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
121                      this.columnHeader1,
122                      this.columnHeader2});
123   this.listView1.Location = new System.Drawing.Point(248, 40);
124   this.listView1.Name = "listView1";
125   this.listView1.Size = new System.Drawing.Size(184, 288);
126   this.listView1.TabIndex = 5;
127   //
128   // columnHeader1
129   //
130   this.columnHeader1.Text = "dddd";
131   //
132   // columnHeader2
133   //
134   this.columnHeader2.Text = "sssss";
135   //
136   // Form1
137   //
138   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
139   this.ClientSize = new System.Drawing.Size(544, 413);
140   this.Controls.Add(this.listView1);
141   this.Controls.Add(this.progressBarSearch);
142   this.Controls.Add(this.button1);
143   this.Controls.Add(this.textBox1);
144   this.Name = "Form1";
145   this.Text = "Form1";
146   this.ResumeLayout(false);
147
148  }
149  #endregion
150
151  /// <summary>
152  /// 应用程序的主入口点。
153  /// </summary>
154  [STAThread]
155  static void Main()
156  {
157   Application.Run(new Form1());
158  }
159  private void button1_Click(object sender, System.EventArgs e)
160  {
161
162   LanSearch();
163
164  }
165  /// <summary>
166  /// 局域网搜索事件
167  /// </summary>
168  private void LanSearch()
169  {
170   thread = new Thread[255];
171
172   ThreadStart threadMethod;
173
174   Thread threadProgress = new Thread(new ThreadStart(progressSearch));
175   threadProgress.Start();
176
177   string localhost = (Dns.GetHostByName(Dns.GetHostName())).AddressList[0].ToString();  //本地主机IP地址
178   str = localhost.Substring(0,localhost.LastIndexOf("."));
179
180   for (int i=0;i<255;i++)  //建立255个线程扫描IP
181   {
182    threadMethod = new ThreadStart(LanSearchThreadMethod);
183    thread[i] = new Thread(threadMethod);
184    thread[i].Name = i.ToString();
185    thread[i].Start();
186    if (!thread[i].Join(100))    //Thread.Join(100)不知道这处这么用对不对,感觉没什么效果一样
187    {
188     thread[i].Abort();
189    }
190   }
191
192   GetLanHost();
193   listLanHost();
194  }
195  /// <summary>
196  /// 多线程搜索方法
197  /// </summary>
198  private void LanSearchThreadMethod()
199  {
200   int Currently_i = Convert.ToUInt16(Thread.CurrentThread.Name);  //当前进程名称
201
202   IPAddress ScanIP = IPAddress.Parse( str + "."+Convert.ToString(Currently_i +1));  //获得扫描IP地址
203   IPHostEntry ScanHost = null;
204   ScanHost = Dns.GetHostByAddress(ScanIP);   //获得扫描IP地址主机信息
205
206   if (ScanHost != null)
207   {
208    LanHost[Currently_i,0] = ScanIP.ToString();
209    LanHost[Currently_i,1] = ScanHost.HostName;
210   }
211
212   //progressBarSearch.Value = progressBarSearch.Value +1;
213
214  }
215  /// <summary>
216  /// 文本框显示主机名与IP列表
217  /// </summary>
218  private void GetLanHost()
219  {
220   for (int i=0;i<255;i++)
221    if ( LanHost[i,0] !="")
222    {
223     textBox1.Text =textBox1.Text + LanHost[i,1] +":" +LanHost[i,0] + "/r/n";
224    }
225  }
226  /// <summary>
227  /// listview1 显示搜索主机
228  /// </summary>
229  private void listLanHost()
230  {
231   listView1.View = View.List;
232
233   ListViewItem aa ;
234   for (int i=0;i<255;i++)
235   {
236    if ( LanHost[i,0] !="")
237    {
238     aa= new ListViewItem();
239     aa.Text = LanHost[i,1];
240     aa.Tag = LanHost[i,0];
241     listView1.Items.Add(aa);
242    }
243   }
244
245  }
246  /// <summary>
247  /// 进度条处理线程
248  /// </summary>
249  private void progressSearch()
250  {
251   //label1.Text = "进度条只是时间估计,不是真实搜索进度!";
252   progressBarSearch.Value = 0;
253   for (int i=0;i<255;i++)
254   {
255    progressBarSearch.Value = progressBarSearch.Value + 1;
256    Thread.Sleep(100);
257   }
258  }
259 }
260}
261
262遗憾之处:因搜索较慢,没有实现真实的搜索进度。
263不懂之处:实现文字提示时,当在鼠标事件首尾插入
264private void button1_Click(object sender, System.EventArgs e)
265  {
266    lab1.Text = “开始搜索”;      //新插入
267   LanSearch();
268    lab1.Text = “结束搜索”;     //新插入
269  }
270文本提示时,在lab1上始终不能及时显示,而是等所有线程结束后才显示“结束搜索“。
271初学winForm编程,太多的要学了。
272望高手指点一二
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: