简体   繁体   中英

Launching a process in C# and passing an xml file

I have the following code to run a cmd line based solver.

//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. How can I feed in my .xml correctly, and have the information from the cmd line feed into my strOutput successfully?

You need to set StartInfo.WorkingDirectory to the right place before running the subprocess. I suspect the current directory of the process is probably that of the output directory in visual studio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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