Winform获取本地IP和外网IP

       try
            {
                const string ip138ComIcAsp = "http://iframe.ip138.com/ic.asp"; //查询IP138得到您当前的外网IP
                var uri = new Uri(ip138ComIcAsp);
                WebRequest wr = WebRequest.Create(uri);
                Stream stream = wr.GetResponse().GetResponseStream();
                if (stream != null)
                {
                    //外网IP
                    var reader = new StreamReader(stream, Encoding.Default);
                    string result = reader.ReadToEnd(); //读取网站的数据
                    Match ip = Regex.Match(result, @"(?<=(\[))[\s\S]*?(?=(\]))");

                    //内网IP
                    IPHostEntry entry = Dns.GetHostByName(Dns.GetHostName());
                    var address = new IPAddress(entry.AddressList[0].Address);

                    LblCurrentIP.Text = string.Format("外网IP:{0} 内网IP:{1}", ip, address);
                }
                LblDataBase.Text += ConfigHelper.GetDBConConfig().DB_ServerName;
            }
            catch (Exception ex)
            {
                // Log(ex.Message);
            }


你可能感兴趣的:(WinForm)