How do I hide a console application user interface when using Process.Start?

后端 未结 4 1438
挽巷
挽巷 2021-02-14 19:59

I want to run a console application that will output a file.

I user the following code:

Process barProcess = Process.Start(\"bar.exe\", @\"C:\\foo.txt\")         


        
4条回答
  •  隐瞒了意图╮
    2021-02-14 20:43

    If you would like to retrieve the output of the process while it is executing, you can do the following (example uses the 'ping' command):

    var info = new ProcessStartInfo("ping", "stackoverflow.com") {
        UseShellExecute = false, 
        RedirectStandardOutput = true, 
        CreateNoWindow = true 
    };
    var cmd = new Process() { StartInfo = info };
    cmd.Start();
    var so = cmd.StandardOutput;
    while(!so.EndOfStream) {
        var c = ((char)so.Read()); // or so.ReadLine(), etc
        Console.Write(c); // or whatever you want
    }
    ...
    cmd.Dispose(); // Don't forget, or else wrap in a using statement
    

提交回复
热议问题