Console & form program

dosborn278

New member
Joined
Jul 20, 2005
Messages
3
Programming Experience
3-5
I'm trying to create a program that can run via the command line or in GUI. If the user runs the the program and passes a -s option it should not open up the form and instead output text to the current console window. Unforunately I can't figure out how to output to the console window. A Console.WriteLine doesn't work unless the program is setup as a console program. If I set it up as a console program and the user chooses to start it in GUI then the console window still hangs in the background.
 
I don't think this will work. If the user starts the program from the command line I'm thinking this would open a new console window instead of outputing to the current one. There must be a way to just output to the current console window if one is open.
 
A commandline is a commandline is a commandline. Whether you start your app from the console window, the Run command on the Start Menu or a desktop shortcut, you are executing a commandline. As far as I'm aware, your app has no idea whether that commandline was executed within a console window or not.

If you use my suggestion, you could have two apps, e.g. MyAppC.exe (console) and MyAppW.exe (forms). If you run "MyAppC.exe" in a console window, it runs like any other console app. If you run "MyAppC.exe -w" in a console window, the console app starts, calls Process.Start("MyAppW.exe"), then exits. If you run "MyAppW.exe" it runs the WinForms app like any other. If you run "MyAppW.exe -c" the WinForms app starts, calls Process.Start("MyAppC.exe"), then exits. There could also be an option in each program to switch to the other at any time. The state of the caller could be saved to XML or whatever so the other app could pick up where it left off.
 
My startup module is sub main. It is a generic module that checks for command line arguments. If it doesn't get a -s then it creates an instance of the form and displays it, otherwise it does Console.WriteLine. Unforunately the Console.Writeline don't go anywhere, even if the program was started at the cmd prompt
 
Back
Top