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

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 窗体设计器生成的代码   ...  #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望高手指点一二
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值