Launching a process in C# and passing an xml file
I have the following code to run a cmd line based solver.
//Create process
var pProcess = new System.Diagnostics.Process();
//strCommand is path and file name of command to run
const string strCommand = // where .bat file is
pProcess.StartInfo.FileName = strCommand;
//strCommandParameters are parameters to pass to program
string strCommandParameters = " -xml '" + xmlFile +"'";
pProcess.StartInfo.Arguments = strCommandParameters;
pProcess.StartInfo.UseShellExecute = false;
//Set output of program to be written to process output stream
pProcess.StartInfo.RedirectStandardOutput = true;
//Start the process
pProcess.Start();
//Get program output
this.StrOutput = pProcess.StandardOutput.ReadToEnd();
//Wait for process to finish
pProcess.WaitForExit();
When I run it as is it exceptions out that it cannot find the file I am passing to it, when I comment out the UseShellExecute and RedirectStandardOutput it runs as expected but does not feed the information to my this.StrOutput, when I comment out the useShellExecute the redirect complains that the useShellExecute is not set to false. How can I feed in my .xml correctly, and have the information from 开发者_运维知识库the cmd line feed into my strOutput successfully?
You need to set StartInfo.WorkingDirectory to the right place before running the subprocess. I suspect the current directory of the process is probably that of the output directory in visual studio.
精彩评论