110,538
社区成员
发帖
与我相关
我的任务
分享
private string CmdPing(string strip)
{
// 实例一个Process类,启动一个独立进程
Process p = new Process();
// 设定程序名
p.StartInfo.FileName = "cmd.exe";
// 关闭Shell的使用
p.StartInfo.UseShellExecute = false;
// 重定向标准输入
p.StartInfo.RedirectStandardInput = true;
// 重定向标准输出
p.StartInfo.RedirectStandardOutput = true;
//重定向错误输出
p.StartInfo.RedirectStandardError = true;
// 设置不显示窗口
p.StartInfo.CreateNoWindow = true;
//启动进程
string pingrst;
p.Start();
//让进程执行CMD命令
p.StandardInput.WriteLine("ping -n 1 " + strip);
p.StandardInput.WriteLine("exit");
//从输出流获取命令执行结果
string strRst = p.StandardOutput.ReadToEnd();
if (strRst.IndexOf("(0% loss)") != -1)
pingrst = "连接";
else pingrst = "异常";
p.Close();
return pingrst;
}
private static string pingResult(string str)
{
Ping ping = new Ping();
PingReply reply = ping.Send(str, 1000);
if (reply.Status == IPStatus.Success)
{
return "连接成功!";
}
else
{
return "连接失败!";
}
}