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

C# 用C启动进程并传递xml文件,c#,C#,我有以下代码来运行基于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 =

我有以下代码来运行基于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。如何才能正确地输入my.xml,并将cmd行输入的信息成功地输入到strOutput中?

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

我正在输入完整的路径C:\。。。。对于.bat文件和.xml文件。我是否遗漏了一些内容,或者如果我这样做了,我应该避免设置工作目录吗?在调用子流程之前,您是否尝试过在这段代码中测试文件路径System.IO.File.Existsfile