作者: cskin
查看: 2948|回复: 7
打印 上一主题 下一主题

[教程] 【系统操作篇】取本机内,外网IP

[复制链接]
跳转到指定楼层
楼主
cskin 发表于 2014-8-1 21:38:00 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
查看: 2948|回复: 7
[C#] 纯文本查看 复制代码
//得到本机内网IP

string s = "";//定义一个变量,用来记录IP地址
System.Net.IPAddress[] addressList = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList;//获取IP地址列表
for (int i = 0; i < addressList.Length; i++)//遍历IP地址列表
{
    s += addressList[i].ToString() + "\n";//获得遍历到的IP地址
}
//s就是本机内网ip


[C#] 纯文本查看 复制代码
/// <summary>
/// 获得本机公网IP
/// </summary>
/// <returns>获取不到则返回空串</returns>
private static string GetIP()
{
    string tempip = "";
    try
    {
        WebRequest wr = WebRequest.Create("http://www.ip138.com/ips138.asp");
        Stream s = wr.GetResponse().GetResponseStream();
        StreamReader sr = new StreamReader(s, Encoding.Default);
        string all = sr.ReadToEnd(); //读取网站的数据

        int start = all.IndexOf("您的IP地址是:[") + 9;
        int end = all.IndexOf("]", start);
        tempip = all.Substring(start, end - start);
        sr.Close();
        s.Close();
    }
    catch
    {
    }
    return tempip;
}




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 转播转播
回复 论坛版权

使用道具 举报

您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-5-3 18:18, Processed in 0.572659 second(s), 28 queries , Gzip On.

快速回复 返回顶部 返回列表