2

I have a C# Console application project where the output type is set to "Windows application". This is so the console does not flash up at the start of the program.

However i also want to allow a help command line argument which will display details about the program if you run it from the command line with "/?" as an argument.

Is there a way to have the program run as a windows application normally but show a console if the help argument is passed?

EDIT - After reading the answers and a similar one at This question (this question assumes you are running with a console application output type) I am using this solution.

 [DllImport(Kernel32_DllName)] private static extern bool AllocConsole(); [DllImport("kernel32.dll")] static extern IntPtr GetConsoleWindow(); [DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); const int SW_HIDE = 0; const int SW_SHOW = 5; static void Main(string[] args) { if(args.Contains("/?")) { AllocConsole(); Console.WriteLine("helpText"); Console.ReadLine(); var handle = GetConsoleWindow(); //Hides console ShowWindow(handle, SW_HIDE); } } 
1

3 Answers 3

2

I have not found a way to do exactly as you are asking, but you can have a console open up depending on the input. For example:

class Program { private const string Kernel32_DllName = "kernel32.dll"; [DllImport(Kernel32_DllName)] private static extern bool AllocConsole(); static void Main(string[] args) { if (args[0] == "/") { AllocConsole(); Console.WriteLine("Details"); Console.ReadKey(); //cases and such for your menu options } 

That will open a console that you can use if you follow the run command with a / even though the output type of the project is a Windows Application.

Sign up to request clarification or add additional context in comments.

Comments

1

Is there a way to have the program run as a windows application normally but show a console if the help argument is passed?

For what you want, learn about using command line arguments here. Basically declare Main to accept arguments as an array of string:

static void Main(string[] args) 

Use a simple dialog form to display the help message or even a MessageBox which is a very simple dialog form.

This gives you much better control rather than trying to cobble something together that wasn't really meant to be put together.

Comments

0

When I need to do stuff like this, I usually make my app a console app. I then parse the args if any in main and decide if I'm going to hide the console window and launch my UI or write to the console.

Hiding a window is fairly straightforward. See How to hide / unhide a process in C#? for an example.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.