.net中c#获取本机IP地址实例代码
* 在使用前,一定要注意在头部加上引用:
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地址实例代码的更多相关文章
- Linux下获取本机IP地址的代码
Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...
- java获取本机IP地址
转载自:http://blog.csdn.net/thunder09/article/details/5360251 在网上找了几个用java获取本机IP地址的代码,发现都少都有些不完美,自己整理了一 ...
- shell中获取本机ip地址
shell中获取本机ip地址 方法一: /sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr ...
- 关于是用dotnet获取本机IP地址+计算机名的方法
印象中在maxscript帮助文档里找到过方法,但是当时没记下来.只能通过dotnet实现了. 如果电脑有无线网卡和本地连接,可能会出现乱码,也问了写dotnet的朋友,提供了一些思路,不过最终还是使 ...
- QT5下获取本机IP地址、计算机名、网络连接名、MAC地址、子网掩码、广播地址
获取主机名称 /* * 名称:get_localmachine_name * 功能:获取本机机器名称 * 参数:no * 返回:QString */ QString CafesClient::get_ ...
- Python - 获取本机IP地址、Mac地址
Python - 获取本机IP地址.Mac地址 在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 直接看代码: # Python - 获取本机I ...
- 获取本机IP地址
这里有两种方法: //获取本机IP - (NSString *)localIPAddress { NSString *localIP = nil; struct ifaddrs *addrs; ) { ...
- Windows下获取本机IP地址方法介绍
Windows下获取本机IP地址方法介绍 if((hostinfo = gethostbyname(name)) != NULL) { #if 1 ; printf("IP COUNT: % ...
- C# 获取本机IP地址以及转换字符串
/// <summary> /// IP地址转化 /// </summary> /// <param name="ipaddr">整型的IP地址 ...
随机推荐
- 【转】SVN提交一般原则
写在前面: 最近, 跳槽了. 来到了一家感觉QIAN景不错的创业公司. 由于是开始, 规模比较小, SVN的管理工作, 也有我来承担. 今天发生了一些相当不愉快的事情, 就是因为一个同事, ①签入代码 ...
- mysql innodb 性能优化
建议参数: max_connections=800 key_buffer_size=512M query_cache_size=128M sort_buffer_size=64M table_open ...
- 修改 EF的默认连接工厂为 Sql Server 而不是LocalDb
当你用EF6创建一个新项目,不知你是否注意到默认的连接字符串使用了LocalDb而不是SQLServer.但你如果想把默认连接改用SQLSErver而不是LocalDb.这个其实很简单:只需修改下 ...
- CSS控制TD内的文本超出指定宽度后不换行而用省略号代替
<style type="text/css"> .lineOverflow { width: 100px; border:#000 solid 1px; text-ov ...
- IP地址查询接口及调用方法
1.查询地址 搜狐IP地址查询接口(IP):http://pv.sohu.com/cityjson 1616 IP地址查询接口(IP+地址):http://w.1616.net/chaxun/ipto ...
- Computer assisted surgery
Computer assisted surgery (CAS) represents a surgical concept and set of methods, that use computer ...
- vtk工作流
要理解VTK的工作原理,首先应明确几个类型: 1.vtkSource(数据源) 这个就好比一个剧本里面的角色,让演员知道要演的是什么人物. 数据源有:vtkConeSource,vtkSphere ...
- SharePoint Fundation 2013中SecurityTokenServiceApplication错误
在Fundation 2013与Office Web Apps Server集成,预览文档时提示错误,存入口检查失败,因为可用内存(47091712 字节)少于总内存的 5%.因此,该服务不可用于传入 ...
- 更改UIsearchbar 的背景和cancel按钮(转)
修改背景 searchbar =[[UISearchBar alloc]initWithFrame:CGRectMake(,KTopBarHeight, , KTopBarHeight)]; sear ...
- 解决:error: Cannot fetch repo (TypeError: expected string or buffer)
同步源码,问题重现: Fetching project platform/external/libopus Fetching project repo error: Cannot fetch repo ...