Windows でリモート システムに ping すると応答がないと表示されますが、C# で ping すると成功と表示されます。Windows は正常ですが、デバイスは接続されていません。Windows では ping できないのに、コードでは正常に ping できるのはなぜですか?
これが私のコードです:
Ping p1 = new Ping();
PingReply PR = p1.Send("192.168.2.18");
// check when the ping is not success
while (!PR.Status.ToString().Equals("Success"))
{
Console.WriteLine(PR.Status.ToString());
PR = p1.Send("192.168.2.18");
}
// check after the ping is n success
while (PR.Status.ToString().Equals("Success"))
{
Console.WriteLine(PR.Status.ToString());
PR = p1.Send("192.168.2.18");
}
ベストアンサー1
using System.Net.NetworkInformation;
public static bool PingHost(string nameOrAddress)
{
bool pingable = false;
Ping pinger = null;
try
{
pinger = new Ping();
PingReply reply = pinger.Send(nameOrAddress);
pingable = reply.Status == IPStatus.Success;
}
catch (PingException)
{
// Discard PingExceptions and return false;
}
finally
{
if (pinger != null)
{
pinger.Dispose();
}
}
return pingable;
}