C
C#2y ago
mixels

❔ Create console app with runtime option to run headless or with console in .NET 6 or later?

I want to build an application that gives the user running it the option to display the console or to not display the console (headless console). I know that InteropServices is capable of hiding the window. This is a mostly satisfactory solution, except it's kind of ugly because running the application does briefly display the console. I'm wondering, is there a way in .NET 6 or later to specify when running a console application to hide or show the console?
7 Replies
ZacharyPatten
ZacharyPatten2y ago
what is your OutputType on your project? if you use WinExe instead of Exe then it will not show the console if you run it by double clicking it or whatever but, you might also want to consider making your application a service instead of a typical exe if that might suite it better
mixels
mixelsOP2y ago
Yeah but I don't want to use a property of the project. I want to give a runtime option to users of the app. As far as I know, Exe always shows and WinExe always hides
ZacharyPatten
ZacharyPatten2y ago
how would you dynamically let the user show/hide the console? what kinda of application are we talking about
mixels
mixelsOP2y ago
command line, either env variable or arg it's a bot that maintains a websocket connection with remote host and has a web api
ZacharyPatten
ZacharyPatten2y ago
There are multiple ways to do what you are trying to do, but why not have different apps? 1. the primary functionality of the bot in a WinExe app that is hidden 2. a monitoring app that is a Exe that prints out whatever output you want to see user could launch and close the monitoring app as they see fit
Jester
Jester2y ago
in windows if its launching in the old cmd window (not the new terminal) you can pinvoke GetConsoleWindow and ShowWindow to hide and show the console window trough code. oh i guess you already knew. the other option is starting without a console and using AllocConsole to get a console window
Accord
Accord2y ago
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.

Did you find this page helpful?