欢迎光临
我们一直在努力

C#查本机地址和网段扫描-.NET教程,数据库应用

建站超值云服务器,限时71元/月

using system;

using system.drawing;

using system.collections;

using system.componentmodel;

using system.windows.forms;

using system.net;

using system.threading;

namespace windowsapplication1

{

/// <summary>

/// form1 的摘要说明。

/// </summary>

public class form1 : system.windows.forms.form

{

private system.windows.forms.button button1;

private system.windows.forms.button button2;

private system.windows.forms.button button3;

private system.windows.forms.textbox textbox1;

private system.windows.forms.textbox textbox2;

private system.windows.forms.label label1;

private system.windows.forms.textbox textbox3;

private system.windows.forms.textbox textbox4;

private system.windows.forms.textbox textbox5;

/// <summary>

/// 必需的设计器变量。

/// </summary>

private system.componentmodel.container components = null;

public form1()

{

//

// windows 窗体设计器支持所必需的

//

initializecomponent();

//

// todo: 在 initializecomponent 调用后添加任何构造函数代码

//

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.dispose();

}

}

base.dispose( disposing );

}

public static void main()

{

application.run(new form1());

}

#region windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 – 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void initializecomponent()

{

this.button1 = new system.windows.forms.button();

this.button2 = new system.windows.forms.button();

this.button3 = new system.windows.forms.button();

this.textbox1 = new system.windows.forms.textbox();

this.textbox2 = new system.windows.forms.textbox();

this.label1 = new system.windows.forms.label();

this.textbox3 = new system.windows.forms.textbox();

this.textbox4 = new system.windows.forms.textbox();

this.textbox5 = new system.windows.forms.textbox();

this.suspendlayout();

//

// button1

//

this.button1.location = new system.drawing.point(40, 272);

this.button1.name = "button1";

this.button1.size = new system.drawing.size(104, 32);

this.button1.tabindex = 0;

this.button1.text = "button1";

this.button1.click += new system.eventhandler(this.button1_click);

//

// button2

//

this.button2.location = new system.drawing.point(264, 272);

this.button2.name = "button2";

this.button2.size = new system.drawing.size(104, 32);

this.button2.tabindex = 1;

this.button2.text = "button2";

this.button2.click += new system.eventhandler(this.button2_click);

//

// button3

//

this.button3.location = new system.drawing.point(480, 272);

this.button3.name = "button3";

this.button3.size = new system.drawing.size(104, 32);

this.button3.tabindex = 2;

this.button3.text = "button3";

this.button3.click += new system.eventhandler(this.button3_click);

//

// textbox1

//

this.textbox1.location = new system.drawing.point(40, 88);

this.textbox1.multiline = true;

this.textbox1.name = "textbox1";

this.textbox1.size = new system.drawing.size(544, 64);

this.textbox1.tabindex = 3;

this.textbox1.text = "";

//

// textbox2

//

this.textbox2.location = new system.drawing.point(40, 184);

this.textbox2.multiline = true;

this.textbox2.name = "textbox2";

this.textbox2.size = new system.drawing.size(544, 56);

this.textbox2.tabindex = 4;

this.textbox2.text = "";

//

// label1

//

this.label1.location = new system.drawing.point(40, 24);

this.label1.name = "label1";

this.label1.size = new system.drawing.size(120, 24);

this.label1.tabindex = 5;

this.label1.text = "label1";

//

// textbox3

//

this.textbox3.location = new system.drawing.point(256, 16);

this.textbox3.name = "textbox3";

this.textbox3.size = new system.drawing.size(176, 21);

this.textbox3.tabindex = 6;

this.textbox3.text = "textbox3";

//

// textbox4

//

this.textbox4.location = new system.drawing.point(256, 56);

this.textbox4.name = "textbox4";

this.textbox4.size = new system.drawing.size(176, 21);

this.textbox4.tabindex = 7;

this.textbox4.text = "textbox4";

//

// textbox5

//

this.textbox5.location = new system.drawing.point(464, 56);

this.textbox5.name = "textbox5";

this.textbox5.size = new system.drawing.size(152, 21);

this.textbox5.tabindex = 8;

this.textbox5.text = "textbox5";

//

// form1

//

this.autoscalebasesize = new system.drawing.size(6, 14);

this.clientsize = new system.drawing.size(648, 350);

this.controls.add(this.textbox5);

this.controls.add(this.textbox4);

this.controls.add(this.textbox3);

this.controls.add(this.label1);

this.controls.add(this.textbox2);

this.controls.add(this.textbox1);

this.controls.add(this.button3);

this.controls.add(this.button2);

this.controls.add(this.button1);

this.name = "form1";

this.text = "form1";

this.load += new system.eventhandler(this.form1_load);

this.resumelayout(false);

}

#endregion

private void form1_load(object sender, system.eventargs e)

{

}

private void button1_click(object sender, system.eventargs e)

{

iphostentry myhost = new iphostentry();

try

{

myhost = dns.gethostbyname(dns.gethostname());

//显示本地主机名

this.textbox2.appendtext(myhost.hostname.tostring());

//显示本地主机的ip地址表

for(int i=0; i < myhost.addresslist.length; i++)

{

this.textbox2.appendtext("本地主机ip地址->" + myhost.addresslist[i].tostring() + "\r\n");

textbox2.refresh();

}

}

catch(exception error)

{

messagebox.show(error.message);

}

}

private void button2_click(object sender, system.eventargs e)

{

iphostentry mydnstoip = new iphostentry();

mydnstoip = dns.resolve(this.textbox1.text);

//显示此域名的ip地址的列表

for(int i = 0; i < mydnstoip.addresslist.length; i++)

{

this.textbox2.appendtext(this.textbox1.text + "的ip地址是" + mydnstoip.addresslist[i].tostring() + "\r\n");

}

}

private void button3_click(object sender, system.eventargs e)

{

thread thscan = new thread(new threadstart(scantarget));

thscan.name="threadtest";

thscan.start();

}

private void scantarget()

{

this.button3.enabled = false;

this.label1.text = "0";

string stripaddress = textbox3.text.tostring();

//开始扫描地址

int nstrat = int32.parse(textbox4.text);

//终止扫描地址

int nend = int32.parse(textbox5.text);

//扫描的操作

for(int i = nstrat; i <= nend; i++)

{

string strscanipadd = stripaddress + "."+i.tostring();

this.label1.text = i.tostring();

//转换成ip地址

ipaddress myscanip = ipaddress.parse(strscanipadd);

try

{

//你可以加入自已的,增强功能

// dns.gethostbyaddress 方法: 根据 ip 地

//址获取 dns 主机信息。

iphostentry myscanhost = dns.gethostbyaddress(myscanip);

//获取主机的名

string strhostname = myscanhost.hostname.tostring();

this.textbox2.appendtext(strscanipadd + " -> " + strhostname + "\r\n");

}

catch(exception error)

{

messagebox.show(error.message);

}

}

this.button3.enabled = true;

}

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » C#查本机地址和网段扫描-.NET教程,数据库应用
分享到: 更多 (0)

相关推荐

  • 暂无文章