C# 从Start.Process重定向输出

C# 从Start.Process重定向输出,c#,C#,当使用Process.Start()调用命令行程序时,我正在尝试重定向该程序的输出。我尝试了RedirectStandardOutput、UseShellExecute和CreateNowWindow的所有排列,但没有成功 我知道还有其他方法可以实现这一点,比如在这之后读取StandardOutput,但如果可能的话,我更愿意使用参数将其重定向到文件中。我只关心返回代码,只需要myprog.exe的输出就可以转到文件。我的程序不需要知道输出是什么 var p = new Process(); p

当使用
Process.Start()
调用命令行程序时,我正在尝试重定向该程序的输出。我尝试了
RedirectStandardOutput
UseShellExecute
CreateNowWindow
的所有排列,但没有成功

我知道还有其他方法可以实现这一点,比如在这之后读取
StandardOutput
,但如果可能的话,我更愿意使用参数将其重定向到文件中。我只关心返回代码,只需要myprog.exe的输出就可以转到文件。我的程序不需要知道输出是什么

var p = new Process();
p.StartInfo.FileName = @"myprog.exe";
p.StartInfo.Arguments = " " + InputFilename + " > " + OutputFilename;

p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;

p.Start();
p.WaitForExit();
var result = p.ExitCode;

这对我的问题有帮助。使用p.StandardOutput.ReadToEnd()获取输出并将其写入文件

这回答了你的问题吗?不是真的。我只关心从myprog.exe到文件的输出。我的程序不需要对myprog.exe的输出执行任何操作,只需要获取返回值。您是否尝试了
cmd.exe
用于
p.StartInfo.FileName
myprog.exe inputfileName>outputFileName
用于
p.StartInfo.Arguments
?我没有想到这一点,但我尝试了,但没有成功。程序已锁定。如果使用重定向(文件重定向,如>文件名),则需要ShellExecute=true,因为这是一个shell(cmd)函数。或者,您可以将命令更改为直接调用shell,如“cmd.exe/c dir>file”