c# process调用cmdexe 接收执行结果
可以使用Process类启动cmd.exe进程,并将其重定向到一个新的Process对象中。然后可以使用该Process对象的StandardOutput属性来读取cmd.exe的输出结果。
以下是一个示例代码:
using System;
using System.Diagnostics;
class Program {
static void Main(string[] args) {
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c dir"; // 执行 dir 命令
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string result = process.StandardOutput.ReadToEnd();
Console.WriteLine(result);
process.WaitForExit();
}
}
在上面的代码中,我们启动了一个cmd.exe进程,并执行了dir命令。我们将其重定向到一个新的Process对象中,并使用StandardOutput属性读取输出结果。最后,我们等待该进程退出
原文地址: https://www.cveoy.top/t/topic/f3Lw 著作权归作者所有。请勿转载和采集!