簡體   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