c#check domain is live

Dat*_*ase 4 c# networking hostname

我想知道如何检查域名是否在c#中存在?

我做的

IPAddress[] addresslist = Dns.GetHostAddresses("domain");
Run Code Online (Sandbox Code Playgroud)

它返回IP,这是好的,但我想检查它是否是实时的(如ping)?

我只需要知道PC是否与服务器在同一网络中,所以我有一个功能来检查但是基于上面的代码的功能,它返回IP但我注意到它是基于缓存的DNS!

我想添加另一个检查功能,看看服务器是否在线!

干杯

Joe*_*lly 6

System.Net.NetworkInformation.OperationalStatus属性让你检测,如果你的网络接口连接(最高).

NetworkInterface.GetIPProperties方法会给你的IP信息.

您可以使用它来检查计算机是否在局域网上,然后执行您需要的操作 - 如ping - 如果已连接.


Alb*_*nbo 5

您可以使用该Ping.Send(IPAddress)方法。

编辑:或者直接采用主机名的重载之一。 Ping.Send(string)

来自 MSDN 的示例:

Ping pingSender = new Ping ();
PingReply reply = pingSender.Send ("www.contoso.com");
if (reply.Status == IPStatus.Success)
{
    Console.WriteLine ("Address: {0}", reply.Address.ToString ());
    Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
    Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
    Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
    Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
}
else
{
    Console.WriteLine (reply.Status);
}
Run Code Online (Sandbox Code Playgroud)