博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 获取当前ip
阅读量:7026 次
发布时间:2019-06-28

本文共 940 字,大约阅读时间需要 3 分钟。

1。获取局域网ip      

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

2。获取公网ip      

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

3.如果是ADSL上网,获取公网ip  

string tempIP =string.Empty; if (System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.Length >1)      tempIP = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[1].ToString();

转载于:https://www.cnblogs.com/lbonet/p/6963832.html

你可能感兴趣的文章
网页基础
查看>>
在Oracle中设置主键自增
查看>>
正则表达式(括号)、[中括号]、{大括号}的区别小结
查看>>
HTML基础第十讲---排版卷标
查看>>
88.NODE.JS加密模块CRYPTO常用方法介绍
查看>>
java.net.ProtocolException: Exceeded stated content-length of: '13824' bytes
查看>>
asp.net 连接 oracle10g 数据库
查看>>
C 入门 第十一节
查看>>
HTML简单的注册页面搭建
查看>>
例23:选择排序
查看>>
【06】Vue 之 组件化开发
查看>>
分类算法评价标准
查看>>
word转HTML在layuiadmin中锚点调转问题
查看>>
代码整洁之道——3、对象和数据结构
查看>>
HTML5
查看>>
[转] Express 4 中的变化
查看>>
润乾在东方通tongweb5.0上部署手册
查看>>
怎样制作爽心的 dashboard ?
查看>>
MySQL查询语句练习题(50题版)
查看>>
Docker 安装
查看>>