Executing Command line .exe with parameters in C#

前端 未结 3 1709
夕颜
夕颜 2020-12-06 17:49

I\'m trying to execute a command line program with parameters from C#. I would have imagined that standing this up and making this happen would be trivial in C# but its prov

3条回答
  •  日久生厌
    2020-12-06 18:34

    Wait for the process to end (let it do its work):

    ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
    
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    procStartInfo.CreateNoWindow = true;
    
    // wrap IDisposable into using (in order to release hProcess) 
    using(Process process = new Process()) {
      process.StartInfo = procStartInfo;
      process.Start();
    
      // Add this: wait until process does its work
      process.WaitForExit();
    
      // and only then read the result
      string result = process.StandardOutput.ReadToEnd();
      Console.WriteLine(result);
    }
    

提交回复
热议问题