* 在使用前,一定要注意在头部加上引用:

using System.Net;

代码如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.Write(new Program().GetHostInfo());
Console.ReadLine();
}
//获取本地IP等信息
protected string GetHostInfo()
{
StringBuilder Info = new StringBuilder("");
IPAddress[] ipHost = Dns.GetHostAddresses(Dns.GetHostName());
Info.Append("本机名:");
Info.Append(Dns.GetHostName());
Info.Append(" -> ");
Info.Append("IP 地址:");
Info.Append(" -> ");
foreach (IPAddress address in ipHost)
{
Info.Append(address.ToString());
Info.Append(" >> ");
}
return Info.ToString();
}
}
}

 * 但是以上代码还不能满足 程序员的要求,想要的也许就是一个192.168.1.66这样的IP,所以需要进行过滤筛选,代码如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
String sd = new Program().GetLocalIP();
Console.WriteLine(sd);
Console.ReadLine();
}
/// <summary>
/// 获取本地IP等信息
/// </summary>
/// <returns></returns>
private string GetLocalIP()
{
//本机IP地址
string strLocalIP = "";
//得到计算机名
string strPcName = Dns.GetHostName();
//得到本机IP地址数组
IPHostEntry ipEntry = Dns.GetHostEntry(strPcName);
//遍历数组
foreach (var IPadd in ipEntry.AddressList)
{
//判断当前字符串是否为正确IP地址
if (IsRightIP(IPadd.ToString()))
{
//得到本地IP地址
strLocalIP = IPadd.ToString();
//结束循环
break;
}
} //返回本地IP地址
return strLocalIP;
}
/// <summary>
/// 判断是否为正确的IP地址
/// </summary>
/// <param name="strIPadd">IP</param>
/// <returns></returns>
public static bool IsRightIP(string strIPadd)
{
//利用正则表达式判断字符串是否符合IPv4格式
if (Regex.IsMatch(strIPadd, @"^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$"))
{
//根据小数点分拆字符串
string[] ips = strIPadd.Split('.');
if (ips.Length == || ips.Length == )
{
//如果符合IPv4规则
if (System.Int32.Parse(ips[]) < && System.Int32.Parse(ips[]) < & System.Int32.Parse(ips[]) < & System.Int32.Parse(ips[]) < )
{
if(IsPingIP(strIPadd))
//正确
return true;
else
//错误
return false;
} //如果不符合
else
//错误
return false;
}
else
//错误
return false;
}
else
//错误
return false;
}
/// <summary>
/// 尝试Ping指定IP 是否能够Ping通
/// </summary>
/// <param name="strIP">IP</param>
/// <returns></returns>
public static bool IsPingIP(string strIP)
{
try
{
//创建Ping对象
Ping ping = new Ping();
//接受Ping返回值
PingReply reply = ping.Send(strIP, );
//Ping通
return true;
}
catch
{
//Ping失败
return false;
}
}
}
}

点我看代码~

* 参考:http://www.111cn.net/net/160/47427.htm

.net中c#获取本机IP地址实例代码的更多相关文章

  1. Linux下获取本机IP地址的代码

    Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...

  2. java获取本机IP地址

    转载自:http://blog.csdn.net/thunder09/article/details/5360251 在网上找了几个用java获取本机IP地址的代码,发现都少都有些不完美,自己整理了一 ...

  3. shell中获取本机ip地址

    shell中获取本机ip地址 方法一: /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr ...

  4. 关于是用dotnet获取本机IP地址+计算机名的方法

    印象中在maxscript帮助文档里找到过方法,但是当时没记下来.只能通过dotnet实现了. 如果电脑有无线网卡和本地连接,可能会出现乱码,也问了写dotnet的朋友,提供了一些思路,不过最终还是使 ...

  5. QT5下获取本机IP地址、计算机名、网络连接名、MAC地址、子网掩码、广播地址

    获取主机名称 /* * 名称:get_localmachine_name * 功能:获取本机机器名称 * 参数:no * 返回:QString */ QString CafesClient::get_ ...

  6. Python - 获取本机IP地址、Mac地址

    Python - 获取本机IP地址.Mac地址 在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 直接看代码: # Python - 获取本机I ...

  7. 获取本机IP地址

    这里有两种方法: //获取本机IP - (NSString *)localIPAddress { NSString *localIP = nil; struct ifaddrs *addrs; ) { ...

  8. Windows下获取本机IP地址方法介绍

    Windows下获取本机IP地址方法介绍 if((hostinfo = gethostbyname(name)) != NULL) { #if 1 ; printf("IP COUNT: % ...

  9. C# 获取本机IP地址以及转换字符串

    /// <summary> /// IP地址转化 /// </summary> /// <param name="ipaddr">整型的IP地址 ...

随机推荐

  1. C程序运行计时

    在标准的C/C++中最小的时间单位是毫秒ms,下面代码中clock_t是long: 每经过1ms clock()的值就增加1:常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元 ...

  2. MyBatis入门案例、增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

  3. BZOJ2595[WC2008]游览计划

    Description Input 第一行有两个整数,N和 M,描述方块的数目. 接下来 N行, 每行有 M 个非负整数, 如果该整数为 0, 则该方块为一个景点:否则表示控制该方块至少需要的志愿者数 ...

  4. jQuery的getText()方法源码

    /** * Utility function for retrieving the text value of an array of DOM nodes * @param {Array|Elemen ...

  5. Intellij Idea中的Jetty报出Web application not found src/main/webapp错误的解决方案

    今天在Intellij Idea中编译项目的时候,运行起来一直会报出如下的错误: Web application not found src/main/webapp 当时感觉应该是什么文件缺少了.所以 ...

  6. 中文乱码?不,是 HTML 实体编码!

    When question comes 在 如何用 Nodejs 分析一个简单页面 一文中,我们爬取了博客园首页的 20 篇文章标题,输出部分拼接了一个字符串: var $ = cheerio.loa ...

  7. JS组件系列——Bootstrap寒冬暖身篇:弹出框和提示框效果以及代码展示

    前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的 ...

  8. learn mips

    可以使用MARS来编汇编,MARS是一个用java编的IDE,它是一个模拟环境. 样例:重要的句子输出三遍 .data str: .asciiz "weidiao is great\n&qu ...

  9. 使用对话框 —— Dialog

      对话框就是一般的弹出窗口,主要用来提示用户,和用户交互.   创建Activity对话框 使用Activity模拟对话框.这个比较简单,主要是使用Activity自带的Dialog主题.   创建 ...

  10. 安裝 14.04.1 Ubuntu 到 Lenovo thinkpad t460p

    在 Lenovo Thinkpad T460p 安裝 ubuntu, BIOS 需要做一些設定, 沒設定的現象:不斷地停在 usb disk 設定 可以 使用 usb disk install 了!