Process获取CMD命令的输出信息



我试图得到我的CMD命令的输出,我得到错误的输出:

这是我的CMD命令:cm whoami

这是我应该得到的输出(CMD输出):

C:UsersJoevin>cm whoami
JoevinFerret

这是我的代码:

Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "cm whoami",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
string outpout = process.StandardOutput.ReadLine();

是我得到的输出:outpout = "Microsoft Windows [Version 10.0.19041.804]"

感谢Mathias。R和Christian。我已经找到解决办法了。

下面是我的代码:

Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c cm whoami",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
string outpout = process.StandardOutput.ReadToEnd();
process.WaitForExit();

最新更新