从C#控制台应用程序我打开命令提示符并检查ping实用程序。
string aptracommand;
aptracommand = "/C ping 10.38.2.73";
Process.Start(@"cmd",aptracommand);
现在,我需要应用一个条件语句,如果ping请求超时,那么它应该说“无法连接”,如果它能够ping,那么需要显示“Server is up”
答案 0 :(得分:9)
您可以将Ping类用于此目的。如下所述:
using System.Net.NetworkInformation;
var ping = new Ping();
var reply = ping.Send("10.38.2.73", 60 * 1000); // 1 minute time out (in ms)
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Server is up");
}
else
{
Console.WriteLine("Server is down");
}
您可以减少超时值,以快速检查服务器是启动还是关闭。
答案 1 :(得分:2)
使用此代替
Ping pingSender = new Ping ();
byte[] data = Encoding.ASCII.GetBytes ("test");
int timeout = 100;
PingReply reply = pingSender.Send("127.0.0.1", timeout, data);
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);
}
答案 2 :(得分:0)
我认为你需要捕获输出,检查是否存在超时字符串,然后根据它做出决定。