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

c#得到本机内网ip、外网ip

时间:2018-03-30 17:20:26      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:ade   公网   one   bre   response   end   new   close   eth   

内网
 IPAddress ipAddr = Dns.Resolve(Dns.GetHostName()).AddressList[0];//获得当前IP地址
            string ip = ipAddr.ToString();

外网

 //获取本机的公网IP
        public static string GetPublicNetworkIP()
        {
            string tempip = "";
            WebRequest request = WebRequest.Create("http://ip.qq.com");
            request.Timeout = 10000;
            WebResponse response = request.GetResponse();
            Stream resStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
            string htmlinfo = sr.ReadToEnd();
            //匹配IP的正则表达式
            Regex r = new Regex("((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|[1-9])", RegexOptions.None);
            Match mc = r.Match(htmlinfo);
            //获取匹配到的IP
            tempip = mc.Groups[0].Value;
            resStream.Close();
            sr.Close();
            return tempip;
        }

 

c#得到本机内网ip、外网ip

标签:ade   公网   one   bre   response   end   new   close   eth   

原文地址:https://www.cnblogs.com/gaocong/p/8677133.html

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