C# 从windows应用程序中运行命令行可执行文件时重定向标准输出时出现问题
好的,正如标题所暗示的,我在这方面遇到了一些麻烦。。。。当我使用以下代码时,它将运行,但我甚至不能使用>output.txt来获取它运行的一些状态C# 从windows应用程序中运行命令行可执行文件时重定向标准输出时出现问题,c#,C#,好的,正如标题所暗示的,我在这方面遇到了一些麻烦。。。。当我使用以下代码时,它将运行,但我甚至不能使用>output.txt来获取它运行的一些状态 ProcessStartInfo x = new ProcessStartInfo(); x.FileName = "somefile.exe"; x.Arguments = arg1 + " " + arg2 + " " + arg3 + " " + arg4;
ProcessStartInfo x = new ProcessStartInfo();
x.FileName = "somefile.exe";
x.Arguments = arg1 + " " + arg2 + " " + arg3 + " " + arg4;
x.WorkingDirectory = workDir;
x.WindowStyle = ProcessWindowStyle.Hidden;
Process mde = Process.Start(x);
mde.WaitForExit();
现在,让我困惑的是,当我添加捕获输入的代码时,我被抛出一个异常,声明我试图运行的exe文件不存在。所以当我使用
ProcessStartInfo x = new ProcessStartInfo();
x.FileName = "somefile.exe";
x.Arguments = arg1 + " " + arg2 + " " + arg3 + " " + arg4;
x.WorkingDirectory = workDir;
x.WindowStyle = ProcessWindowStyle.Hidden;
modelf.UseShellExecute = false;
modelf.RedirectStandardOutput = true;
Process mde = Process.Start(x);
mde.WaitForExit();
我到底做错了什么。这就像在使用useshellexecute属性时无法设置working directory属性一样,但据我所知,情况并非如此。发生了什么事?为什么它能在第一个示例中找到文件并正确执行,而在第二个示例中却不能找到文件并正确执行?MSDN引自
当UseShellExecute为false时
未使用WorkingDirectory属性
查找可执行文件。相反,它是
由已启动的进程使用
而且只有在
新进程的背景
万一有人想知道它的工作方式
Process x = new Process
{
StartInfo =
{
FileName = fullPathToExe,
Arguments = arg1 + " " + arg2 + " " + arg3 + " " + arg4,
WorkingDirectory = outDir,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true
}
};
x.Start();
string output = x.StandardOutput.ReadToEnd();
x.WaitForExit();
它仍然会在窗口中闪烁,但我认为createnowindow=true会解决这个问题。我想我会发布代码,以防其他人需要它。我也需要为参数添加完整路径吗?nvm发现我只需要将路径添加到filename属性。很好,谢谢。