执行效果,“Go”获得外网IP,下边的textbox显示连接网站后返回的html代码!外网IP就是从中提取的。
主文件 GetIP.cs
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Management;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace IPGet
... {
public partial class Form_IP : Form
...{
public Form_IP()
...{
InitializeComponent();
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
...{
if (!(bool)mo["IPEnabled"])
continue;
this.textBoxMAC.Text +=" --- "+ mo["MACAddress"].ToString();
string[] addresses = (string[])mo["IPAddress"];
string[] subnets = (string[])mo["IPSubnet"];
// display IP Addresses
foreach (string sad in addresses)
this.textBoxLocalIP.Text += " --- " + sad + " ";
// display SubNets mask
foreach (string sub in subnets)
this.textBoxSub.Text += " --- " + sub + " ";
}
}
private string GetIP()
...{
Uri uri = new Uri("http://www.ciker.net/ip/index.asp");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 0;
req.CookieContainer = new System.Net.CookieContainer();
req.GetRequestStream().Write(new byte[0], 0, 0);
HttpWebResponse res = (HttpWebResponse)(req.GetResponse());
StreamReader rs = new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
try
...{
string s = rs.ReadToEnd();
this.textBox.Text = s;
rs.Close();
req.Abort();
res.Close();
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(s, @"您的IP:(?<IP>[0-9.]*)");
if (m.Success)
...{
return m.Groups["IP"].Value;
}
else
...{
return "没有连接上主机";
}
}
catch (Exception e)
...{
return "获取失败!";
}
}
private void buttonGo_Click(object sender, EventArgs e)
...{
this.textBoxIP.Text = GetIP();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Management;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace IPGet
... {
public partial class Form_IP : Form
...{
public Form_IP()
...{
InitializeComponent();
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
...{
if (!(bool)mo["IPEnabled"])
continue;
this.textBoxMAC.Text +=" --- "+ mo["MACAddress"].ToString();
string[] addresses = (string[])mo["IPAddress"];
string[] subnets = (string[])mo["IPSubnet"];
// display IP Addresses
foreach (string sad in addresses)
this.textBoxLocalIP.Text += " --- " + sad + " ";
// display SubNets mask
foreach (string sub in subnets)
this.textBoxSub.Text += " --- " + sub + " ";
}
}
private string GetIP()
...{
Uri uri = new Uri("http://www.ciker.net/ip/index.asp");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 0;
req.CookieContainer = new System.Net.CookieContainer();
req.GetRequestStream().Write(new byte[0], 0, 0);
HttpWebResponse res = (HttpWebResponse)(req.GetResponse());
StreamReader rs = new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("GB2312"));
try
...{
string s = rs.ReadToEnd();
this.textBox.Text = s;
rs.Close();
req.Abort();
res.Close();
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(s, @"您的IP:(?<IP>[0-9.]*)");
if (m.Success)
...{
return m.Groups["IP"].Value;
}
else
...{
return "没有连接上主机";
}
}
catch (Exception e)
...{
return "获取失败!";
}
}
private void buttonGo_Click(object sender, EventArgs e)
...{
this.textBoxIP.Text = GetIP();
}
}
}
附:窗口设计代码:
namespace
IPGet
... {
partial class Form_IP
...{
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
...{
if (disposing && (components != null))
...{
components.Dispose();
}
base.Dispose(disposing);
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.buttonGo = new System.Windows.Forms.Button();
this.textBoxIP = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.textBoxLocalIP = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.textBoxMAC = new System.Windows.Forms.TextBox();
this.textBoxSub = new System.Windows.Forms.TextBox();
this.textBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// buttonGo
//
this.buttonGo.Location = new System.Drawing.Point(231, 4);
this.buttonGo.Name = "buttonGo";
this.buttonGo.Size = new System.Drawing.Size(75, 23);
this.buttonGo.TabIndex = 0;
this.buttonGo.Text = "GO";
this.buttonGo.UseVisualStyleBackColor = true;
this.buttonGo.Click += new System.EventHandler(this.buttonGo_Click);
//
// textBoxIP
//
this.textBoxIP.Location = new System.Drawing.Point(59, 6);
this.textBoxIP.Name = "textBoxIP";
this.textBoxIP.Size = new System.Drawing.Size(143, 21);
this.textBoxIP.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 2;
this.label1.Text = "本地IP";
//
// textBoxLocalIP
//
this.textBoxLocalIP.Location = new System.Drawing.Point(59, 37);
this.textBoxLocalIP.Name = "textBoxLocalIP";
this.textBoxLocalIP.Size = new System.Drawing.Size(143, 21);
this.textBoxLocalIP.TabIndex = 3;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 9);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 2;
this.label2.Text = "外网IP";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(30, 102);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(23, 12);
this.label3.TabIndex = 2;
this.label3.Text = "MAC";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(0, 71);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(53, 12);
this.label4.TabIndex = 2;
this.label4.Text = "子网掩码";
//
// textBoxMAC
//
this.textBoxMAC.Location = new System.Drawing.Point(59, 99);
this.textBoxMAC.Name = "textBoxMAC";
this.textBoxMAC.Size = new System.Drawing.Size(143, 21);
this.textBoxMAC.TabIndex = 3;
//
// textBoxSub
//
this.textBoxSub.Location = new System.Drawing.Point(59, 68);
this.textBoxSub.Name = "textBoxSub";
this.textBoxSub.Size = new System.Drawing.Size(143, 21);
this.textBoxSub.TabIndex = 3;
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(59, 152);
this.textBox.Multiline = true;
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(436, 174);
this.textBox.TabIndex = 4;
//
// Form_IP
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.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
private System.Windows.Forms.Button buttonGo;
private System.Windows.Forms.TextBox textBoxIP;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBoxLocalIP;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBoxMAC;
private System.Windows.Forms.TextBox textBoxSub;
private System.Windows.Forms.TextBox textBox;
}
}
... {
partial class Form_IP
...{
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
...{
if (disposing && (components != null))
...{
components.Dispose();
}
base.Dispose(disposing);
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.buttonGo = new System.Windows.Forms.Button();
this.textBoxIP = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.textBoxLocalIP = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.textBoxMAC = new System.Windows.Forms.TextBox();
this.textBoxSub = new System.Windows.Forms.TextBox();
this.textBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// buttonGo
//
this.buttonGo.Location = new System.Drawing.Point(231, 4);
this.buttonGo.Name = "buttonGo";
this.buttonGo.Size = new System.Drawing.Size(75, 23);
this.buttonGo.TabIndex = 0;
this.buttonGo.Text = "GO";
this.buttonGo.UseVisualStyleBackColor = true;
this.buttonGo.Click += new System.EventHandler(this.buttonGo_Click);
//
// textBoxIP
//
this.textBoxIP.Location = new System.Drawing.Point(59, 6);
this.textBoxIP.Name = "textBoxIP";
this.textBoxIP.Size = new System.Drawing.Size(143, 21);
this.textBoxIP.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 2;
this.label1.Text = "本地IP";
//
// textBoxLocalIP
//
this.textBoxLocalIP.Location = new System.Drawing.Point(59, 37);
this.textBoxLocalIP.Name = "textBoxLocalIP";
this.textBoxLocalIP.Size = new System.Drawing.Size(143, 21);
this.textBoxLocalIP.TabIndex = 3;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 9);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 2;
this.label2.Text = "外网IP";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(30, 102);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(23, 12);
this.label3.TabIndex = 2;
this.label3.Text = "MAC";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(0, 71);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(53, 12);
this.label4.TabIndex = 2;
this.label4.Text = "子网掩码";
//
// textBoxMAC
//
this.textBoxMAC.Location = new System.Drawing.Point(59, 99);
this.textBoxMAC.Name = "textBoxMAC";
this.textBoxMAC.Size = new System.Drawing.Size(143, 21);
this.textBoxMAC.TabIndex = 3;
//
// textBoxSub
//
this.textBoxSub.Location = new System.Drawing.Point(59, 68);
this.textBoxSub.Name = "textBoxSub";
this.textBoxSub.Size = new System.Drawing.Size(143, 21);
this.textBoxSub.TabIndex = 3;
//
// textBox
//
this.textBox.Location = new System.Drawing.Point(59, 152);
this.textBox.Multiline = true;
this.textBox.Name = "textBox";
this.textBox.Size = new System.Drawing.Size(436, 174);
this.textBox.TabIndex = 4;
//
// Form_IP
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.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
private System.Windows.Forms.Button buttonGo;
private System.Windows.Forms.TextBox textBoxIP;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBoxLocalIP;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBoxMAC;
private System.Windows.Forms.TextBox textBoxSub;
private System.Windows.Forms.TextBox textBox;
}
}