在c#中启动一个进程并传递一个xml文件

本文关键字:一个 xml 文件 进程 启动 | 更新日期: 2023-09-27 18:09:05

我有以下代码来运行基于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成功的信息?

在c#中启动一个进程并传递一个xml文件

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