How To: Execute command line in C#, get STD OUT results

后端 未结 17 1238
既然无缘
既然无缘 2020-11-21 13:12

How do I execute a command-line program from C# and get back the STD OUT results? Specifically, I want to execute DIFF on two files that are programmatically selected and wr

17条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-21 14:07

    Here's a quick sample:

    //Create process
    System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
    
    //strCommand is path and file name of command to run
    pProcess.StartInfo.FileName = strCommand;
    
    //strCommandParameters are parameters to pass to program
    pProcess.StartInfo.Arguments = strCommandParameters;
    
    pProcess.StartInfo.UseShellExecute = false;
    
    //Set output of program to be written to process output stream
    pProcess.StartInfo.RedirectStandardOutput = true;   
    
    //Optional
    pProcess.StartInfo.WorkingDirectory = strWorkingDirectory;
    
    //Start the process
    pProcess.Start();
    
    //Get program output
    string strOutput = pProcess.StandardOutput.ReadToEnd();
    
    //Wait for process to finish
    pProcess.WaitForExit();
    

提交回复
热议问题