2

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.)

Vaccano
  • 78,325
  • 149
  • 468
  • 850
  • I think this is a good solution for your problem. [http://stackoverflow.com/questions/3563744/c-hiding-a-console-window][1] [1]: http://stackoverflow.com/questions/3563744/c-hiding-a-console-window – Talke Aug 11 '11 at 14:42
  • I think this is the solution for your problem : [http://stackoverflow.com/questions/3563744/c-hiding-a-console-window][1] [1]: http://stackoverflow.com/questions/3563744/c-hiding-a-console-window – Talke Aug 11 '11 at 14:43
  • 1
    @Talke: Not quite a duplicate – the other Q doesn't include the "only sometimes" part. – Richard Aug 11 '11 at 14:53

2 Answers2

2

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.)

Richard
  • 106,783
  • 21
  • 203
  • 265
-1

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).

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • FreeConsole has the same problems as ShowWindow(handle, SW_HIDE). It only helps for an already existing console. I want it to never appear in the first place. – Vaccano Aug 11 '11 at 14:45
  • Read the "Another solution" part. The fact is that you can never hide a console from start when you have specified "console application" as project type. – jgauffin Aug 11 '11 at 14:47