gpt4 book ai didi

c# - 在 C# 中验证 ping 实用程序

转载 作者:行者123 更新时间:2023-11-30 19:11:14 25 4
gpt4 key购买 nike

在 C# 控制台应用程序中,我打开了命令提示符并检查了 ping 实用程序。

string aptracommand;
aptracommand = "/C ping 10.38.2.73";
Process.Start(@"cmd",aptracommand);

现在,我需要应用一个条件语句,如果 ping 请求超时,那么它应该说“无法连接”,如果它能够 ping 则需要显示“服务器已启动”

最佳答案

您可以为此目的使用 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");
}

您可以减少超时值以快速检查服务器是启动还是关闭。

关于c# - 在 C# 中验证 ping 实用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12620915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com