C# 程序——获得外网IP,MAC,本地IP,子网掩码实例

执行效果,“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], 00);
            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 窗体设计器生成的代码

        
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;
    }

}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值