Code
/// <summary>
/// 根据IP地址获得主机名称
/// </summary>
/// <param name="ip">主机的IP地址</param>
/// <returns>主机名称</returns>
public string GetHostNameByIp(string ip)
{
ip = ip.Trim();
if (ip == string.Empty)
return string.Empty;
try
{
// 是否 Ping 的通
if (this.Ping(ip))
{
System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(ip);
return host.HostName;
}
else
return string.Empty;
}
catch (Exception)
{
return string.Empty;
}
}
/// <summary>
/// 根据IP地址获得主机名称
/// </summary>
/// <param name="ip">主机的IP地址</param>
/// <returns>主机名称</returns>
public string GetHostNameByIp(string ip)
{
ip = ip.Trim();
if (ip == string.Empty)
return string.Empty;
try
{
// 是否 Ping 的通
if (this.Ping(ip))
{
System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(ip);
return host.HostName;
}
else
return string.Empty;
}
catch (Exception)
{
return string.Empty;
}
}
Code
/// <summary>
/// 是否能 Ping 通指定的主机
/// </summary>
/// <param name="ip">ip 地址或主机名或域名</param>
/// <returns>true 通,false 不通</returns>
public bool Ping(string ip)
{
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true;
string data = "Test Data!";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 1000; // Timeout 时间,单位:毫秒
System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
return true;
else
return false;
}
/// <summary>
/// 是否能 Ping 通指定的主机
/// </summary>
/// <param name="ip">ip 地址或主机名或域名</param>
/// <returns>true 通,false 不通</returns>
public bool Ping(string ip)
{
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
options.DontFragment = true;
string data = "Test Data!";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 1000; // Timeout 时间,单位:毫秒
System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
if (reply.Status == System.Net.NetworkInformation.IPStatus.Success)
return true;
else
return false;
}
Code
/// <summary>
/// 根据主机名(域名)获得主机的IP地址
/// </summary>
/// <param name="hostName">主机名或域名</param>
/// <example>GetIPByDomain("pc001"); GetIPByDomain("www.google.com");</example>
/// <returns>主机的IP地址</returns>
public string GetIpByHostName(string hostName)
{
hostName = hostName.Trim();
if (hostName == string.Empty)
return string.Empty;
try
{
System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(hostName);
return host.AddressList.GetValue(0).ToString();
}
catch (Exception)
{
return string.Empty;
}
}
/// <summary>
/// 根据主机名(域名)获得主机的IP地址
/// </summary>
/// <param name="hostName">主机名或域名</param>
/// <example>GetIPByDomain("pc001"); GetIPByDomain("www.google.com");</example>
/// <returns>主机的IP地址</returns>
public string GetIpByHostName(string hostName)
{
hostName = hostName.Trim();
if (hostName == string.Empty)
return string.Empty;
try
{
System.Net.IPHostEntry host = System.Net.Dns.GetHostEntry(hostName);
return host.AddressList.GetValue(0).ToString();
}
catch (Exception)
{
return string.Empty;
}
}