我尝试在命令行中为命令运行 process.start(),并尝试将输出转换为字符串或一些有用的位置。输出将包含几行(如 DIR 命令)。我阅读了操作方法,但它对我不起作用。它运行但随后进入循环并且不会停止。见下文。有什么想法吗?
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("cmd.exe",
@" /k dir");
Process myProcess = new Process();
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = @" /k dir";
myProcess.Start();
string ppp = myProcess.StandardOutput.ReadToEnd();
myProcess.WaitForExit();
最佳答案
您的进程永远不会自行退出,因为您将 /K
标志传递给 cmd.exe
,这将使窗口保持打开状态(因此进程处于事件状态) ) 在它运行你的命令之后。
/K Carries out the command specified by string but remains
我认为您在这里寻找的是使用 /C
,它将在运行您的命令后关闭命令窗口。
/C Carries out the command specified by string and then terminates
关于c# - 如何从我的命令行捕获标准输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9517830/