c#ping很多主机的最佳方式

时间:2015-03-11 21:25:36

标签: c# ping

我试图运行CMD并从那里获得ping信息:

    ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
    cmdStartInfo.FileName = @"cmd";
    cmdStartInfo.RedirectStandardOutput = true;
    cmdStartInfo.RedirectStandardError = true;
    cmdStartInfo.RedirectStandardInput = true;
    cmdStartInfo.UseShellExecute = false;
    cmdStartInfo.CreateNoWindow = true;

    Process cmd = new Process();
    cmd.StartInfo = cmdStartInfo;
    cmd.ErrorDataReceived += DataReceived;
    cmd.OutputDataReceived += DataReceived;
    cmd.EnableRaisingEvents = true;
    cmd.Start();
    cmd.BeginOutputReadLine();
    cmd.BeginErrorReadLine();

    cmd.StandardInput.WriteLine("ping www.bing.com");
    cmd.StandardInput.WriteLine("exit");

    System.Threading.Thread.Sleep(4000);
    cmd.WaitForExit();

存在一些更好的方法吗?

1 个答案:

答案 0 :(得分:0)

首先确保您确实需要打开shell来处理命令。在您的示例中,直接运行ping会更简单,例如,

ProcessStartInfo cmdStartInfo = new ProcessStartInfo()
{
    FileName = "ping",
    Arguments = "www.bing.com",
    RedirectStandardOutput = true,
    CreateNoWindow = true,
    UseShellExecute = false
};

Process proc = Process.Start(cmdStartInfo);
string reply = proc.StandardOutput.ReadToEnd();

其中ReadToEnd将等到进程完成后再返回,为您提供ping的整个输出。 ReadToEnd也适用于您的示例,而不是Sleep

我认为这只是一个人为的例子,但如果没有,你不需要像这样使用ping - .net提供了更容易使用的ping类。例如。

private static void Ping(string address = "bing.com", int iterations = 4)
{
    Ping ping = new Ping();
    var hostEntry = Dns.GetHostEntry(address);

    byte[] buffer = new byte[32];
    new Random().NextBytes(buffer);

    Console.WriteLine("Pinging {0} [{1}] with {2} bytes of data:", hostEntry.HostName, hostEntry.AddressList[0], buffer.Length);

    for (int i = 0; i < iterations; i++)
    {
        PingReply reply = ping.Send(address, 1000, buffer);    
        Console.WriteLine("Reply from {0}: bytes={1}, time={2}ms, TTL={3}", reply.Address, reply.Buffer.Length, reply.RoundtripTime, reply.Options.Ttl);
    }
}

编辑:为了避免挂起,你可以这样写。

static void Main()
{
    Task<string> pingReply = RunCommand("ping", "bing.com");

    Console.WriteLine("I am waiting for ping to complete and could be doing something useful");

    pingReply.Wait();

    Console.WriteLine(pingReply.Result);
    Console.ReadKey();
}

public static Task<string> RunCommand(string command, string args)
{
    ProcessStartInfo cmdStartInfo = new ProcessStartInfo()
    {
        FileName = command,
        Arguments = args,
        RedirectStandardOutput = true,
        CreateNoWindow = true,
        UseShellExecute = false
    };

    Process proc = Process.Start(cmdStartInfo);        
    Task<string> readCommand = proc.StandardOutput.ReadToEndAsync();

    return readCommand;
}