0

I have an app which starts another app. This other app prints a few lines into the Console but noone needs this output and it prints it's output betwenn my own. How can I prevent this other app from printing it's stuff into my console? I tried to run with ProcessStartInfo.UseShellExecutive both on true and false, also tried to change the console output into a MemoryStream before starting but since I need the Console i had to change the output back and it looks like the other app got their input changed back too.

Process serverprocess = new Process();
serverprocess.StartInfo.FileName = Path.GetFileName(serverpath);
serverprocess.StartInfo.Arguments = launch;
serverprocess.StartInfo.UseShellExecute = false;
serverprocess.StartInfo.RedirectStandardOutput = true;
serverprocess.Start();
BDevGW
  • 347
  • 3
  • 15
  • how did you redirect the child process stdout to a memorystream? show at least that part of the code. redirection (something like `>nul`) could also work. – Cee McSharpface Feb 13 '20 at 16:26
  • Added.. by you :) – BDevGW Feb 13 '20 at 16:32
  • would this apply: https://stackoverflow.com/a/3642517/1132334 (setting `RedirectStandardOutput` to true, and just not attaching any handler to `OutputDataReceived`)? – Cee McSharpface Feb 13 '20 at 16:35
  • I'm not sure if I understood correctly but I added the redirection and haven'T added anything to OutputDataRecieved but it still shows up in my console :( (Changed the post to current code) – BDevGW Feb 13 '20 at 16:46
  • maybe the child process writes to stderr and not to stdout. redirect the error output as well, there is a separate property for that. related: https://stackoverflow.com/q/29967590/1132334 – Cee McSharpface Feb 13 '20 at 16:53
  • It was the error stream... I hate ARK sometimes. If you post this as an answer I can mark it as correct answer :) – BDevGW Feb 13 '20 at 16:55
  • Does this answer your question? [Process.RedirectStandardOutput does not work](https://stackoverflow.com/questions/29967590/process-redirectstandardoutput-does-not-work) – Cee McSharpface Feb 13 '20 at 16:55
  • It was the error stream, not the output. Now my console is free of ARKs output :) You already told the right thing with the stderr :) Make an answer with this information and I can mark it as solution ^^ – BDevGW Feb 13 '20 at 17:05

1 Answers1

1

In your code ensure that you are re-directing both StandardOutput and StandardError that way everything that the "ThirdPartyApp" writes will be captured in either of these streams.

I have written a small Helper Class that helps with this

You can use like

//Launching excel.exe with /safe as arg
var excelExample1 = @"""C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.EXE"" /safe";

LaunchCMD.Invoke(excelExample1);

//To get its output, if any
var getOutput = LaunchCMD.Output;

LaunchCMD Helper Class

class LaunchCMD
{


    public static string Output
    {
        get; set;
    } = "";




    public static void Invoke(string command, bool waitTillExit = false, bool closeOutputWindow = false)
    {

        ProcessStartInfo ProcessInfo;
        Process Process = new Process();


        ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + command);


        ProcessInfo.CreateNoWindow = false;
        ProcessInfo.UseShellExecute = false;
        ProcessInfo.RedirectStandardOutput = true;
        ProcessInfo.RedirectStandardError = true;
        Process.EnableRaisingEvents = true;

        Process = Process.Start(ProcessInfo);


        Process.ErrorDataReceived += ConsoleDataReceived;
        Process.OutputDataReceived += ConsoleDataReceived;


        Process.BeginOutputReadLine();
        Process.BeginErrorReadLine();




        if (waitTillExit == true)
        {


            Process.WaitForExit();
        }
        if (closeOutputWindow == true)
        {
            Process.CloseMainWindow();
        }


        Process.Close();
        System.Threading.Thread.Sleep(1000);
        Output.ToString();


    }


    private static void ConsoleDataReceived(object sender, DataReceivedEventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        if (e.Data != null)
        {
            Output = Output + e.Data;
        }
    }


}
Clint
  • 6,011
  • 1
  • 21
  • 28
  • I had to redirect the streams but I don't need any attached handler for this streams, I just forgot that the ARK server may also use stderr instead of stdout so I only redirected stdout into nothing :/ – BDevGW Feb 14 '20 at 07:05