c# 获得局域网主机列表实例
2024-07-21 02:18:27
供稿:网友
 
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>
 /// form1 的摘要说明。
 /// </summary>
 public class form1 : system.windows.forms.form
 {
 private system.windows.forms.textbox textbox1;
 private system.windows.forms.button button1;
 private string[,] lanhost;
 private system.windows.forms.progressbar progressbarsearch;
 private thread[] thread;
 private system.windows.forms.listview listview1;
 private system.windows.forms.columnheader columnheader1;
 private system.windows.forms.columnheader columnheader2;
 private string str;
 /// <summary>
 /// 必需的设计器变量。
 /// </summary>
 private system.componentmodel.container components = null;
 public form1()
 {
 //
 // windows 窗体设计器支持所必需的
 //
 initializecomponent();
 initlanhost();
 progressbarsearch.maximum = 255;
 //
 // todo: 在 initializecomponent 调用后添加任何构造函数代码
 //
 }
 /// <summary>
 /// 数组初始化
 /// </summary>
 private void initlanhost()
 {
 lanhost = new string[255,2];
 for (int i=0;i<255;i++)
 {
 lanhost[i,0] = "";
 lanhost[i,1] = "";
 }
 }
 /// <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.textbox1 = new system.windows.forms.textbox();
 this.button1 = new system.windows.forms.button();
 this.progressbarsearch = new system.windows.forms.progressbar();
 this.listview1 = new system.windows.forms.listview();
 this.columnheader1 = new system.windows.forms.columnheader();
 this.columnheader2 = new system.windows.forms.columnheader();
 this.suspendlayout();
 // 
 // textbox1
 // 
 this.textbox1.location = new system.drawing.point(24, 40);
 this.textbox1.multiline = true;
 this.textbox1.name = "textbox1";
 this.textbox1.scrollbars = system.windows.forms.scrollbars.both;
 this.textbox1.size = new system.drawing.size(176, 296);
 this.textbox1.tabindex = 0;
 this.textbox1.text = "";
 // 
 // button1
 // 
 this.button1.location = new system.drawing.point(456, 40);
 this.button1.name = "button1";
 this.button1.tabindex = 1;
 this.button1.text = "开始搜索";
 this.button1.click += new system.eventhandler(this.button1_click);
 // 
 // progressbarsearch
 // 
 this.progressbarsearch.location = new system.drawing.point(32, 360);
 this.progressbarsearch.name = "progressbarsearch";
 this.progressbarsearch.size = new system.drawing.size(490, 24);
 this.progressbarsearch.tabindex = 2;
 // 
 // listview1
 // 
 this.listview1.columns.addrange(new system.windows.forms.columnheader[] {
 this.columnheader1,
 this.columnheader2});
 this.listview1.location = new system.drawing.point(248, 40);
 this.listview1.name = "listview1";
 this.listview1.size = new system.drawing.size(184, 288);
 this.listview1.tabindex = 5;
 // 
 // columnheader1
 // 
 this.columnheader1.text = "dddd";
 // 
 // columnheader2
 // 
 this.columnheader2.text = "sssss";
 // 
 // form1
 // 
 this.autoscalebasesize = new system.drawing.size(6, 14);
 this.clientsize = new system.drawing.size(544, 413);
 this.controls.add(this.listview1);
 this.controls.add(this.progressbarsearch);
 this.controls.add(this.button1);
 this.controls.add(this.textbox1);
 this.name = "form1";
 this.text = "form1";
 this.resumelayout(false);
 }
 #endregion
 /// <summary>
 /// 应用程序的主入口点。
 /// </summary>
 [stathread]
 static void main() 
 {
 application.run(new form1());
 }
 private void button1_click(object sender, system.eventargs e)
 {
 
 lansearch();
 
 }
 /// <summary>
 /// 局域网搜索事件
 /// </summary>
 private void lansearch()
 {
 thread = new thread[255];
 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<255;i++) //建立255个线程扫描ip
 {
 threadmethod = new threadstart(lansearchthreadmethod);
 thread[i] = new thread(threadmethod);
 thread[i].name = i.tostring();
 thread[i].start();
 if (!thread[i].join(100)) //thread.join(100)不知道这处这么用对不对,感觉没什么效果一样
 {
 thread[i].abort();
 }
 }
 getlanhost();
 listlanhost();
 }
 /// <summary>
 /// 多线程搜索方法
 /// </summary>
 private void lansearchthreadmethod()
 {
 int currently_i = convert.touint16(thread.currentthread.name); //当前进程名称
 
 ipaddress scanip = ipaddress.parse( str + "."+convert.tostring(currently_i +1)); //获得扫描ip地址
 iphostentry scanhost = null;
 scanhost = dns.gethostbyaddress(scanip); //获得扫描ip地址主机信息
 if (scanhost != null)
 {
 lanhost[currently_i,0] = scanip.tostring();
 lanhost[currently_i,1] = scanhost.hostname;
 }
 
 //progressbarsearch.value = progressbarsearch.value +1;
 }
 /// <summary>
 /// 文本框显示主机名与ip列表
 /// </summary>
 private void getlanhost()
 {
 for (int i=0;i<255;i++)
 if ( lanhost[i,0] !="")
 {
 textbox1.text =textbox1.text + lanhost[i,1] +":" +lanhost[i,0] + "/r/n";
 }
 }
 /// <summary>
 /// listview1 显示搜索主机
 /// </summary>
 private void listlanhost()
 {
 listview1.view = view.list;
 listviewitem aa ;
 for (int i=0;i<255;i++)
 {
 if ( lanhost[i,0] !="")
 {
 aa= new listviewitem();
 aa.text = lanhost[i,1];
 aa.tag = lanhost[i,0];
 listview1.items.add(aa);
 }
 }
 
 }
 /// <summary>
 /// 进度条处理线程
 /// </summary>
 private void progresssearch()
 {
 //label1.text = "进度条只是时间估计,不是真实搜索进度!";
 progressbarsearch.value = 0;
 for (int i=0;i<255;i++)
 {
 progressbarsearch.value = progressbarsearch.value + 1;
 thread.sleep(100);
 }
 }
 }
}
遗憾之处:因搜索较慢,没有实现真实的搜索进度。
不懂之处:实现文字提示时,当在鼠标事件首尾插入
private void button1_click(object sender, system.eventargs e)
 {
 lab1.text = “开始搜索”; //新插入 
 lansearch();
 lab1.text = “结束搜索”; //新插入
 }
文本提示时,在lab1上始终不能及时显示,而是等所有线程结束后才显示“结束搜索“。
初学winform编程,太多的要学了。
望高手指点一二
网站运营seo文章大全提供全面的站长运营经验及seo技术!