2011-09-20 49 views
0

我有以下代码来运行基于cmd行的求解器。在C#中启动进程并传递一个xml文件

//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(); 

当我运行它是例外的是,它无法找到我传递给它的文件,当我注释掉UseShellExecute和RedirectStandardOutput它运行正常,但信息不进我的这一点。 StrOutput,当我注释掉useShellExecute时,重定向指出useShellExecute未设置为false。我如何正确地提供我的.xml文件,并将cmd行中的信息成功提交到我的strOutput中?

回答

0

在运行子流程之前,您需要将StartInfo.WorkingDirectory设置到正确的位置。我怀疑该进程的当前目录可能是visual studio中输出目录的目录。

+0

我喂完整路径C:\ ....为.bat文件和.xml文件。我是否错过了某些东西,或者我应该能够避免设置工作目录吗? – PlTaylor

+0

您是否曾尝试在调用子进程之前测试此代码中的文件路径? - System.IO.File.Exists(文件) – IanNorton

+0

测试真实,没有任何修改我的nunit测试。 – PlTaylor

相关问题