简体   繁体   English

在C#中启动进程并传递xml文件

[英]Launching a process in C# and passing an xml file

I have the following code to run a cmd line based solver. 我有以下代码来运行基于cmd行的求解器。

//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. 当我按原样运行它时,它无法找到我要传递给它的文件是一个例外,当我注释掉UseShellExecute和RedirectStandardOutput时,它按预期运行,但是当我注释掉它时,不会将信息提供给我的this.StrOutput。 useShellExecute重定向抱怨useShellExecute没有设置为false。 How can I feed in my .xml correctly, and have the information from the cmd line feed into my strOutput successfully? 如何正确输入.xml,并将cmd行中的信息成功输入到strOutput中?

You need to set StartInfo.WorkingDirectory to the right place before running the subprocess. 在运行子流程之前,需要将StartInfo.WorkingDirectory设置在正确的位置。 I suspect the current directory of the process is probably that of the output directory in visual studio. 我怀疑该进程的当前目录可能是Visual Studio中的输出目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM