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

后端 未结 4 1439
挽巷
挽巷 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
    
    0 讨论(0)
  • 2021-02-14 20:46

    We have done this in the past by executing our processes using the Command Line programatically.

    0 讨论(0)
  • 2021-02-14 20:50
            Process p = new Process();
            StreamReader sr;
            StreamReader se;
            StreamWriter sw;
    
            ProcessStartInfo psi = new ProcessStartInfo(@"bar.exe");
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            psi.RedirectStandardInput = true;
            psi.CreateNoWindow = true;
            p.StartInfo = psi;
            p.Start();
    

    This will start a child process without displaying the console window, and will allow the capturing of the StandardOutput, etc.

    0 讨论(0)
  • 2021-02-14 20:58

    Check into ProcessStartInfo and set the WindowStyle = ProcessWindowStyle.Hidden and the CreateNoWindow = true.

    0 讨论(0)
提交回复
热议问题