【发布时间】:2012-08-03 18:02:07
【问题描述】:
当我用 windows ping 远程系统时,它说没有回复,但是当我用 c# ping 时,它说成功。 Windows 是正确的,设备未连接。为什么我的代码在 Windows 不通的情况下能够成功 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");
}
【问题讨论】:
-
当您点击 MSDN 链接 msdn.microsoft.com/en-us/library/… 或 stackoverflow.com/questions/1281176/… 时,查看此页面底部发布的以下示例
-
您应该将 PR.Status 与 IPStatus.Success 进行比较。在这种情况下,字符串比较不是正确的工具。
-
执行 ping 操作后,一些 PingReply 属性的值是多少(如
PR.Address、PR.RoundtripTime、PR.reply.Buffer.Length和PR.Options.Ttl)?另外你确定你的代码中有正确的 IP 地址,而不是测试 IP 地址吗? -
Jon Senchyna :我没有设置它们,是的,我确定我的 IP 是正确的。
-
在我的情况下,如果禁用“启用 Visual Studio 托管进程”(位置 ==>>project-> property->debug),则 ping 方法可能不起作用。请尝试!