码迷,mamicode.com
首页 > Windows程序 > 详细

C# WINFORM获取计算机本机对外IP地址

时间:2015-07-02 11:47:03      阅读:416      评论:0      收藏:0      [点我收藏+]

标签:

 代码中的链接网站是我找的一个获得对外IP地址的网站,可以根据自己需要替换掉

        /// <summary>
        /// 获取对外IP地址
        /// </summary>
        /// <returns></returns>
        public static string GetIP()
        {
            using (var webClient = new WebClient())
            {
                try
                {
                    var temp = webClient.DownloadString("http://1111.ip138.com/ic.asp");
                    var ip = Regex.Match(temp, @"\[(?<ip>\d+\.\d+\.\d+\.\d+)]").Groups["ip"].Value;
                    return !string.IsNullOrEmpty(ip) ? ip : null;
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
        }

C# WINFORM获取计算机本机对外IP地址

标签:

原文地址:http://www.cnblogs.com/CareyZhao/p/4615285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!