前言:
这个IP类,主要是操作本地的IP信息的获取和检查,之前有写一个修改本地IP的方法,但是效率太慢了,被老大给否掉了,就没继续,自我感觉,获取IP信息速度很快,去切换IP(修改IP),感觉很慢(8秒左右),不知道为什么很慢,有了解,请赐教!
代码:
/// <summary>
/// IP地址操作类
/// </summary>
public class Ip
{
/// <summary>
/// 获取本机内网IP地址
/// </summary>
/// <returns>返回:内网IP地址(成功);空字符串(失败)</returns>
public static string GetLocalIpAddress()
{
string result = "";
try
{
foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
{
result = _IPAddress.ToString();
}
}
}
catch (Exception ex)
{
Message.Show(ex.Message);
}
return result;
}
/// <summary>
/// 通过Ping方法查看网络是否联通
/// </summary>
/// <param name="ip">IP地址</param>
/// <returns>是否联通</returns>
public static bool Ping(string ip)
{
bool result = false;
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply pingReply = ping.Send(ip);
if (pingReply.Status == System.Net.NetworkInformation.IPStatus.Success)
{
result = true;
}
}
catch (Exception ex)
{
Message.Show(ex.Message);
}
return result;
}
/// <summary>
/// 通过Ping方法查看网络是否联通
/// </summary>
/// <param name="ip">IP地址</param>
/// <param name="timeOut">超时时间(毫秒)</param>
/// <returns>是否联通</returns>
public static bool Ping(string ip, int timeOut)
{
bool result = false;
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply pingReply = ping.Send(ip, timeOut);
if (pingReply.Status == System.Net.NetworkInformation.IPStatus.Success)
{
result = true;
}
}
catch (Exception ex)
{
Message.Show(ex.Message);
}
return result;
}
}
总结:
其实这个有很简单,也没什么可以说的,只是方便以后,自己想用的时候,能快速找到!其实我觉得积累还是挺有用的,比如我老大提到某一个技术,我之前有用过或积累过,就能很快进行沟通,提高工作效率!