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

后端 未结 17 1599
既然无缘
既然无缘 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:09

    One-liner run command:

    new Process() { StartInfo = new ProcessStartInfo("echo", "Hello, World") }.Start();
    

    Read output of command in shortest amount of reable code:

        var cliProcess = new Process() {
            StartInfo = new ProcessStartInfo("echo", "Hello, World") {
                UseShellExecute = false,
                RedirectStandardOutput = true
            }
        };
        cliProcess.Start();
        string cliOut = cliProcess.StandardOutput.ReadToEnd();
        cliProcess.WaitForExit();
        cliProcess.Close();
    

提交回复
热议问题