繁体   English   中英

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

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

我有以下代码来运行基于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();

当我按原样运行它时,它无法找到我要传递给它的文件是一个例外,当我注释掉UseShellExecute和RedirectStandardOutput时,它按预期运行,但是当我注释掉它时,不会将信息提供给我的this.StrOutput。 useShellExecute重定向抱怨useShellExecute没有设置为false。 如何正确输入.xml,并将cmd行中的信息成功输入到strOutput中?

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

暂无
暂无

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

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