Winform获取本地IP和外网IP

来源:互联网 发布:mac flac音乐转换器 编辑:程序博客网 时间:2024/05/29 08:16

       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);            }


原创粉丝点击