Problem redirecting standard output when running a command line executable from within a windows application
Ok, so as the title implies, I'm having some trouble with this.... When I use the following code it will run but I can't even use > output.txt to get some status of how it ran....
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();
Now, what's confusing me is that the moment I add in the code for capturing the input, I get thrown an exception stating that the exe file I'm trying to run does not exist. So when I use....
ProcessStartInfo x = new ProcessStartInfo();
x.File开发者_Go百科Name = "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();
What exactly am I doing wrong here. It's like the working directory property can't be set when the useshellexecute property is used, but from what I read that's not the case. So what's going on? Why can it find the file and execute it properly in the first example and not in the second?
MSDN Quote from http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.workingdirectory.aspx
When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used by the process that is started and only has meaning within the context of the new process.
In case anyone else wants to know the way it worked....
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();
It still blinks up a window, but I figure createnowindow= true will fix that. I figured I'd post the code in case anyone else needed it.
精彩评论