1.如果你是通过路由上网的,可以通过访问ip138之类的地址来获取外网IP

2.如果是通过PPPOE拨号上网的,可以使用以下代码获取IP

//获取宽带连接(PPPOE拨号)的IP地址,timeout超时(秒),当宽带未连接或者连接中的时候获取不到IP
public static string GetIP_PPPOE(int timeout)
{
int i = timeout * 2;
while (i > 0)
{
try
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
bool havePPPOE = false;
foreach (NetworkInterface adapter in nics)
{
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ppp)
{
havePPPOE = true;
IPInterfaceProperties ip = adapter.GetIPProperties(); //IP配置信息
if (ip.UnicastAddresses.Count > 0)
{
return ip.UnicastAddresses[0].Address.ToString();
}
}
}
//当没有宽带连接的时候直接返回空
if (!havePPPOE) return "";
}
catch (Exception ex)
{
Console.WriteLine("获取宽带拨号IP出错:" + ex.Message);
}
i--;
Thread.Sleep(500);
}
return "";
}

  

c#获取外网IP地址的方法的更多相关文章

  1. java获取外网ip地址

    转自:http://blog.163.com/houjunchang_daxue/blog/static/13037938320134543310451/ /** * 获取外网IP.归属地.操作系统 ...

  2. C# 获取外网IP地址

    很多情况下我们需要获取外网的IP地址,一般用自带的方法获取到的都是不准确,往往获取到的是内网的IP地址,所以需要采用外部网站接口来获取. 代码 通过访问第三方接口来获取真实的ip地址 public s ...

  3. MFC C++ 获取外网IP地址

    #include <afxinet.h> //GB2312 转换成 Unicode wchar_t* GB2312ToUnicode(const char* szGBString) { U ...

  4. linux 获取外网ip地址

    curl ifconfig.me 私有ip地址,获取公网ip

  5. android 根据网络来获取外网ip地址及国家,地区的接口

    新浪的IP地址查询接口:http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 新浪多地域测试方法:http://int.dpool. ...

  6. 服务器查看外网IP地址和方法

    返回IP地址 curl ip..com/ip.aspx curl whatismyip.akamai.com wget -qO - ifconfig.co curl icanhazip.com dig ...

  7. C# Winform程序获取外网IP地址

    string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了 Uri uri = new Uri(strUrl); ...

  8. 获取外网IP地址

    public static string GetRealIP(){            string result = String.Empty;            result = HttpC ...

  9. C#获取外网IP地址;C#获取所在IP城市地址

    public static string GetIP()         {             using (var webClient = new WebClient())           ...

随机推荐

  1. 1229【MySQL】性能优化之 Index Condition Pushdown

    转自http://blog.itpub.net/22664653/viewspace-1210844/  [MySQL]性能优化之 Index Condition Pushdown2014-07-06 ...

  2. neo4j-java连接

    本例使用neo4j3.0.1架包 使用maven下载架包 <!-- https://mvnrepository.com/artifact/org.neo4j/neo4j-jdbc-driver ...

  3. Your app declares support for audio in the UIBackgroundModes key in your Info.plist 错误

    提交AppStore时候被拒绝 拒绝原因:Your app declares support for audio in the UIBackgroundModes key in your Info.p ...

  4. 微软 消息队列 MessageQueue 简单使用

    1.在服务电脑上打开 消息队列 ①进入控制面板>程序>启用或关闭windows功能 ②将需要的勾选(我自己全选了哈哈哈) ③我的电脑 右键 打开管理 见到消息队列 在专用队列上新建专用队列 ...

  5. centos6.5 卸载php

    1.删除php,删除之前需要查看php依赖包,使用 rpm -qa|grep php [root@localhost ~]# rpm -qa |grep phpphp55w-mysql-5.5.38- ...

  6. ES6箭头函数与展开运算符

    箭头函数:省去了关键字function和return: eg: reduce=(a,b)=>a+b;//返回a+b的值 redduce=(a,b)=>{console.log(a);con ...

  7. ubuntu 14.04 修改网络配置

    修改IP地址: vi /etc/network/interfaces

  8. Nginx 和 IIS 实现动静分离

    前段时间,搞Nginx+IIS的负载均衡,想了解的朋友,可以看这篇文章:<nginx 和 IIS 实现负载均衡>,然后也就顺便研究了Nginx + IIS 实现动静分离.所以,一起总结出来 ...

  9. Fatal signal xx (SIGSEGV) at

    Fatal signal 11问题的解决方法 http://blog.csdn.net/tankai19880619/article/details/9004619 如何定位Android NDK开发 ...

  10. 101 LINQ Samples

    https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b