天目网

设置首页 - 加入收藏
首页   女人情感   投资理财   大众美食   宠物乐园   娱乐八卦   电视剧情介绍   电脑技术
 

C#程序——获得外网IP,MAC,本地IP,子网掩码实例-suiying0792的专栏

2008-5-7 19:59:25 来源:本站
 

执行效果,“Go”获得外网IP,下边的textbox显示连接网站后返回的html代码!外网IP就是从中提取的。

主文件GetIP.cs

usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Management;
usingSystem.Windows.Forms;
usingSystem.Net;
usingSystem.IO;

namespaceIPGet
...{
publicpartialclassForm_IP:Form
...{
publicForm_IP()
...{
InitializeComponent();
ManagementClassmc=newManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollectionmoc=mc.GetInstances();
foreach(ManagementObjectmoinmoc)
...{
if(!(bool)mo["IPEnabled"])
continue;
this.textBoxMAC.Text ="---" mo["MACAddress"].ToString();

string[]addresses=(string[])mo["IPAddress"];
string[]subnets=(string[])mo["IPSubnet"];

//displayIPAddresses
foreach(stringsadinaddresses)
this.textBoxLocalIP.Text ="---" sad "";

//displaySubNetsmask
foreach(stringsubinsubnets)
this.textBoxSub.Text ="---" sub "";
}
}

privatestringGetIP()
...{
Uriuri=newUri("http://www.ciker.net/ip/index.asp");
HttpWebRequestreq=(HttpWebRequest)WebRequest.Create(uri);
req.Method="POST";
req.ContentType="application/x-www-form-urlencoded";
req.ContentLength=0;
req.CookieContainer=newSystem.Net.CookieContainer();
req.GetRequestStream().Write(newbyte[0],0,0);
HttpWebResponseres=(HttpWebResponse)(req.GetResponse());
StreamReaderrs=newStreamReader(res.GetResponseStream(),System.Text.Encoding.GetEncoding("GB2312"));
try
...{
strings=rs.ReadToEnd();
this.textBox.Text=s;
rs.Close();
req.Abort();
res.Close();
System.Text.RegularExpressions.Matchm=System.Text.RegularExpressions.Regex.Match(s,@"您的IP:(?<IP>[0-9.]*)");
if(m.Success)
...{
returnm.Groups["IP"].Value;
}
else
...{
return"没有连接上主机";
}
}
catch(Exceptione)
...{
return"获取失败!";
}
}

privatevoidbuttonGo_Click(objectsender,EventArgse)
...{
this.textBoxIP.Text=GetIP();
}
}
}

附:窗口设计代码:

namespaceIPGet
...{
partialclassForm_IP
...{
/**////<summary>
///必需的设计器变量。
///</summary>
privateSystem.ComponentModel.IContainercomponents=null;

/**////<summary>
///清理所有正在使用的资源。
///</summary>
///<paramname="disposing">假如应释放托管资源,为true;否则为false。</param>
protectedoverridevoidDispose(booldisposing)
...{
if(disposing&&(components!=null))
...{
components.Dispose();
}
base.Dispose(disposing);
}

Windows窗体设计器生成的代码#regionWindows窗体设计器生成的代码

/**////<summary>
///设计器支持所需的方法-不要
///使用代码编辑器修改此方法的内容。
///</summary>
privatevoidInitializeComponent()
...{
this.buttonGo=newSystem.Windows.Forms.Button();
this.textBoxIP=newSystem.Windows.Forms.TextBox();
this.label1=newSystem.Windows.Forms.Label();
this.textBoxLocalIP=newSystem.Windows.Forms.TextBox();
this.label2=newSystem.Windows.Forms.Label();
this.label3=newSystem.Windows.Forms.Label();
this.label4=newSystem.Windows.Forms.Label();
this.textBoxMAC=newSystem.Windows.Forms.TextBox();
this.textBoxSub=newSystem.Windows.Forms.TextBox();
this.textBox=newSystem.Windows.Forms.TextBox();
this.SuspendLayout();
//
//buttonGo
//
this.buttonGo.Location=newSystem.Drawing.Point(231,4);
this.buttonGo.Name="buttonGo";
this.buttonGo.Size=newSystem.Drawing.Size(75,23);
this.buttonGo.TabIndex=0;
this.buttonGo.Text="GO";
this.buttonGo.UseVisualStyleBackColor=true;
this.buttonGo.Click =newSystem.EventHandler(this.buttonGo_Click);
//
//textBoxIP
//
this.textBoxIP.Location=newSystem.Drawing.Point(59,6);
this.textBoxIP.Name="textBoxIP";
this.textBoxIP.Size=newSystem.Drawing.Size(143,21);
this.textBoxIP.TabIndex=1;
//
//label1
//
this.label1.AutoSize=true;
this.label1.Location=newSystem.Drawing.Point(12,40);
this.label1.Name="label1";
this.label1.Size=newSystem.Drawing.Size(41,12);
this.label1.TabIndex=2;
this.label1.Text="本地IP";
//
//textBoxLocalIP
//
this.textBoxLocalIP.Location=newSystem.Drawing.Point(59,37);
this.textBoxLocalIP.Name="textBoxLocalIP";
this.textBoxLocalIP.Size=newSystem.Drawing.Size(143,21);
this.textBoxLocalIP.TabIndex=3;
//
//label2
//
this.label2.AutoSize=true;
this.label2.Location=newSystem.Drawing.Point(12,9);
this.label2.Name="label2";
this.label2.Size=newSystem.Drawing.Size(41,12);
this.label2.TabIndex=2;
this.label2.Text="外网IP";
//
//label3
//
this.label3.AutoSize=true;
this.label3.Location=newSystem.Drawing.Point(30,102);
this.label3.Name="label3";
this.label3.Size=newSystem.Drawing.Size(23,12);
this.label3.TabIndex=2;
this.label3.Text="MAC";
//
//label4
//
this.label4.AutoSize=true;
this.label4.Location=newSystem.Drawing.Point(0,71);
this.label4.Name="label4";
this.label4.Size=newSystem.Drawing.Size(53,12);
this.label4.TabIndex=2;
this.label4.Text="子网掩码";
//
//textBoxMAC
//
this.textBoxMAC.Location=newSystem.Drawing.Point(59,99);
this.textBoxMAC.Name="textBoxMAC";
this.textBoxMAC.Size=newSystem.Drawing.Size(143,21);
this.textBoxMAC.TabIndex=3;
//
//textBoxSub
//
this.textBoxSub.Location=newSystem.Drawing.Point(59,68);
this.textBoxSub.Name="textBoxSub";
this.textBoxSub.Size=newSystem.Drawing.Size(143,21);
this.textBoxSub.TabIndex=3;
//
//textBox
//
this.textBox.Location=newSystem.Drawing.Point(59,152);
this.textBox.Multiline=true;
this.textBox.Name="textBox";
this.textBox.Size=newSystem.Drawing.Size(436,174);
this.textBox.TabIndex=4;
//
//Form_IP
//
this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=newSystem.Drawing.Size(521,338);
this.Controls.Add(this.textBox);
this.Controls.Add(this.textBoxSub);
this.Controls.Add(this.textBoxMAC);
this.Controls.Add(this.textBoxLocalIP);
this.Controls.Add(this.label2);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBoxIP);
this.Controls.Add(this.buttonGo);
this.Name="Form_IP";
this.Text="获取外网IP和本地IP和子网掩码";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

privateSystem.Windows.Forms.ButtonbuttonGo;
privateSystem.Windows.Forms.TextBoxtextBoxIP;
privateSystem.Windows.Forms.Labellabel1;
privateSystem.Windows.Forms.TextBoxtextBoxLocalIP;
privateSystem.Windows.Forms.Labellabel2;
privateSystem.Windows.Forms.Labellabel3;
privateSystem.Windows.Forms.Labellabel4;
privateSystem.Windows.Forms.TextBoxtextBoxMAC;
privateSystem.Windows.Forms.TextBoxtextBoxSub;
privateSystem.Windows.Forms.TextBoxtextBox;
}
}

相关文章
·暂无
feedsky
抓虾
pageflakes
Rojo
google reader
netvibes
my yahoo
newsgator
bloglines
鲜果
哪吒
有道