0

I have a C# console application that I am running with its output type set to "Windows Application" to prevent the console from being seen during normal use. However, I would like the option to alternatively run the program as a console application at will, in case the user wanted to troubleshoot and view the console's output.

Is it possible to pass a command-line argument to the executable to either run the application in "Console" mode or "Windows Application" mode depending on the user's desire? If not, is there any other way to change on the fly if an application will show the console or not?

Bobby Byrnes
  • 411
  • 5
  • 18
  • 1
    Might want to look here: http://stackoverflow.com/questions/472282/show-console-in-windows-application – Styxxy Jul 25 '16 at 20:31
  • AllocConsole() is not useful. Create another console mode project and let its Main() method use Process.Start + WaitForExit to start your winapp.exe. When you rename that little program to winapp.com then you can't tell the difference. – Hans Passant Jul 25 '16 at 21:03
  • It is possible. After dotnet publish you can remove the console window. Take a look at here: https://github.com/molnard/WalletWasabi/blob/239a45d14300082675fa70d27dbf47f392b6eec8/WalletWasabi.Packager/Program.cs#L411 – David Molnar Dec 11 '20 at 11:58

2 Answers2

1

I think you have an X-Y Problem. The root problem is that you want the user to be able to run either a console or WinForms program that both do the same thing. One solution would be to have a single program that can run as both, but as @roryap pointed out, this is impossible.

A second solution would be to put the business logic of your program into a separate library. You can then write a console program that accesses this library and a WinForms GUI that also accesses this library. The user then decides which program to run. This is a tried-and-true method of separating an application into multiple layers. I recommend you go this route. You'll find that your user-facing layers (console, WinForms) are small. In the future, if you want to make a Web front-end, or a WPF front-end, you only need to write the front-end part, the business logic layer won't change.

user2023861
  • 8,030
  • 9
  • 57
  • 86
0

No, you can't. There's a fundamental difference between a console application and a winforms application that goes very deep. Once the application is compiled, it cannot be changed at run time.

rory.ap
  • 34,009
  • 10
  • 83
  • 174