0

I'm trying to develop a small Installer-Tool for our application which should be able to simply do its job by double click the EXE. That's the easy part. During the installation a little progress bar informs the user about the whole progress. All fine.. But now I want to use the same installer in another application to verify if an update is available. For that I provided the parameter "--printVersion" within my installer which should then simply print out relevant version information to the console. This is to let the (other) application know if it needs to be upgraded. The other application is starting my installer via

Process installer = new Process();
// ...
installer.StartInfo.RedirectStandardOutput = true;
installer.Start();

and later try to read its response via installer.StandardOutput...

If I keep the type "Windows Application" and if I use Console.WriteLine("...") to print any kind of information to the console, this text is not showing up in the console (tried by starting the app via cmd). If I understand correctly, then a "Windows Application" is detached from the parent (starting) process. This is maybe the reason why I can't see the console output. If I switch to "Console Application" the console output is there and is readable but then a black window if showing while the installer runs which is annoying.

How can I print something to the console but otherwise not showing a black windows if I just double click the EXE from within the explorer?

Martin Fernau
  • 787
  • 1
  • 6
  • 19
  • Try p/invoking `AttachConsole(ATTACH_PARENT_PROCESS)` https://learn.microsoft.com/en-us/windows/console/attachconsole – Ben Voigt Jul 20 '23 at 15:21
  • ...first you have to open stdout with `Console.OpenStandardOutput()` and, e.g, initialize a StreamWriter with it. Attach the Console and then write to the stream. Of course you can also write directly to the Output Stream, but you have to write a byte array (well, it's just `Encoding.Unicode.GetBytes()`) – Jimi Jul 20 '23 at 15:37
  • But, IIRC, after you have called `AttachConsole()` you may also be able to just use `Console.WriteLine()` directly – Jimi Jul 20 '23 at 15:44
  • `installer.StartInfo.CreateNoWindow=true` if you set your installer App to be a console app. Alternatively, if you set your installer app to be a windows app then embrace the fact that there is no console and show messages in a window instead, or in a Dialog if it's just sentence. – Chris F Carroll Jul 20 '23 at 15:56
  • @ChrisFCarroll If the executable is run from the CMD Console, then there's a Console. You can write there, as described. I've added a duplicate that does what I was suggesting almost exactly – Jimi Jul 20 '23 at 16:08

0 Answers0