❔ Output to console if run from console - otherwise do not create console window?
I've got an Windows app that doesn't have a UI. I'd like to not have a console window opened when it's launched fron File Explorer, but if the user launches the app from a terminal (eg.
./myapp.exe
), then I'd want Console.WriteLine
calls (for debugging) to go to that terminal window. Is there some way to achieve this?
If I set the OutputType
to Exe
, then it spawns a console window when launched from File Explorer. However, when run from a terminal window, the behavior is correct - Console.WriteLine
output goes to that existing terminal window.
If I set the OutputType
to WinExe
, the behavior is correct when launched from File Explorer - no console window is spawned. However, when run from a terminal window, Console.WriteLine
calls don't appear at all6 Replies
Try using
AttachConsole(ATTACH_PARENT_PROCESS);
:
OutputType
== WinExe
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
that's working perfectly. thank you!
are you suggesting calling
AttachConsole
conditionally based on Environment.UserInteractive
?Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
cool i'll investigate that 👍 thanks both
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.