国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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技術!
  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 辽阳市| 黑山县| 合阳县| 林州市| 哈尔滨市| 临沂市| 花垣县| 钦州市| 乡宁县| 怀远县| 星子县| 惠州市| 嘉黎县| 昆山市| 绥棱县| 双城市| 广安市| 龙江县| 九龙县| 北川| 门源| 辽阳县| 广宁县| 五大连池市| 天等县| 泗水县| 马鞍山市| 永登县| 集贤县| 隆尧县| 巴南区| 聂拉木县| 基隆市| 黎平县| 射洪县| 丘北县| 寿宁县| 江油市| 无锡市| 利津县| 凉城县|