Vaccano
Vaccano

Reputation: 82467

Hide the console (completely) for a Console app, but only sometimes

I have a C# Console app than runs a pre-build step (to get NuGet Packages).

When I am debugging this, I want to pass in a parameter and show the console. When I am not debugging it I don't want to see it. I don't even want it to flash up there for a second.

I have found ways to hide it, after it has shown. But I can't find a way to never make it show unless I am willing to change it from a console app to a Windows app. (Which I would do if I could then find a way to show the Console when needed.)

Upvotes: 2

Views: 554

Answers (2)

Richard
Richard

Reputation: 109140

Build as a Windows application and show the console when you need it. To show the console when needed use P/Invoke to call AllocConsole (pinvoke.net has the declaration you need).

(Console sub-system processes always get a console, their parent process's if there was one, otherwise a new one. This is the way Windows works at a deep level.)

Upvotes: 2

jgauffin
jgauffin

Reputation: 101176

Use FreeConsole WINAPI function:

http://pinvoke.net/default.aspx/kernel32/FreeConsole.html

Another solution is to simply switch to a WinForms application as project type. No console will be allocated then (and you do not need to show a form).

Upvotes: -1

Related Questions