Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在命令完成后获取命令提示窗口内容_C#_Winforms_Cmd - Fatal编程技术网

C# 在命令完成后获取命令提示窗口内容

C# 在命令完成后获取命令提示窗口内容,c#,winforms,cmd,C#,Winforms,Cmd,我希望在C#中完成一个命令后获得命令提示符窗口的内容 具体来说,在本例中,我通过单击按钮发出ping命令,并希望在文本框中显示输出 我目前使用的代码是: ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe"); startInfo.UseShellExecute = false; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = t

我希望在C#中完成一个命令后获得命令提示符窗口的内容

具体来说,在本例中,我通过单击按钮发出ping命令,并希望在文本框中显示输出

我目前使用的代码是:

ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe");
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.Arguments = "ping 192.168.1.254";
Process pingIt = Process.Start(startInfo);
string errors = pingIt.StandardError.ReadToEnd();
string output = pingIt.StandardOutput.ReadToEnd();

txtLog.Text = "";
if (errors != "")
{
    txtLog.Text = errors;
}
else
{
    txtLog.Text = output;
}
而且它在某种程度上是有效的。它至少获取了一些输出并显示出来,但ping本身并没有执行——或者至少这是我假设的,给定下面的输出,命令提示符窗口会闪烁一秒钟

输出:

Microsoft Windows[版本6.1.7601]版权所有(c)2009 Microsoft 公司版权所有

C:\Checkout\PingUtility\PingUtility\bin\Debug>

非常感谢您的帮助。

这就够了

ProcessStartInfo info = new ProcessStartInfo(); 
info.Arguments = "/C ping 127.0.0.1"; 
info.WindowStyle = ProcessWindowStyle.Hidden; 
info.CreateNoWindow = true; 
info.FileName = "cmd.exe"; 
info.UseShellExecute = false; 
info.RedirectStandardOutput = true; 
using (Process process = Process.Start(info)) 
{ 
    using (StreamReader reader = process.StandardOutput) 
    { 
        string result = reader.ReadToEnd(); 
        textBox1.Text += result; 
    } 
} 
输出


啊,太好了!我至少在某种程度上是正确的。谢谢