Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a C# app that decides itself whether to show as a console or windowed app?

Is there a way to launch a C# application with the following features?

  1. It determines by command-line parameters whether it is a windowed or console app
  2. It doesn't show a console when it is asked to be windowed and doesn't show a GUI window when it is running from the console.

For example,

myapp.exe /help
would output to stdout on the console you used, but
myapp.exe
by itself would launch my Winforms or WPF user interface.

The best answers I know of so far involve having two separate exe and use IPC, but that feels really hacky.


What options do I have and trade-offs can I make to get the behavior described in the example above? I'm open to ideas that are Winform-specific or WPF-specific, too.

like image 377
Matthew Avatar asked Apr 30 '09 17:04

Matthew


People also ask

How do I create a new C project in Visual Studio?

Open Visual Studio, and choose Create a new project in the Start window. In the Create a new project window, select All languages, and then choose C# from the dropdown list. Choose Windows from the All platforms list, and choose Console from the All project types list.

How do I get C in Visual Studio?

1. We need to click on the extension button that displays a sidebar for downloading and installing the C/C++ extension in the visual studio code. In the sidebar, type C Extension. In this image, click on the Install button to install the C/C++ extension.

Can you write C in Visual Studio?

Visual Studio Code is a lightweight, cross-platform development environment that runs on Windows, Mac, and Linux systems. The Microsoft C/C++ for Visual Studio Code extension supports IntelliSense, debugging, code formatting, auto-completion. Visual Studio for Mac doesn't support Microsoft C++, but does support .


2 Answers

Make the app a regular windows app, and create a console on the fly if needed.

More details at this link (code below from there)

using System; using System.Windows.Forms;  namespace WindowsApplication1 {   static class Program {     [STAThread]     static void Main(string[] args) {       if (args.Length > 0) {         // Command line given, display console         if ( !AttachConsole(-1) ) { // Attach to an parent process console            AllocConsole(); // Alloc a new console         }          ConsoleMain(args);       }       else {         Application.EnableVisualStyles();         Application.SetCompatibleTextRenderingDefault(false);         Application.Run(new Form1());       }     }     private static void ConsoleMain(string[] args) {       Console.WriteLine("Command line = {0}", Environment.CommandLine);       for (int ix = 0; ix < args.Length; ++ix)         Console.WriteLine("Argument{0} = {1}", ix + 1, args[ix]);       Console.ReadLine();     }      [System.Runtime.InteropServices.DllImport("kernel32.dll")]     private static extern bool AllocConsole();      [System.Runtime.InteropServices.DllImport("kernel32.dll")]     private static extern bool AttachConsole(int pid);    } } 
like image 81
Eric Petroelje Avatar answered Sep 19 '22 11:09

Eric Petroelje


I basically do that the way depicted in Eric's answer, additionally I detach the console with FreeConsole and use the SendKeys command to get the command prompt back.

    [DllImport("kernel32.dll")]     private static extern bool AllocConsole();      [DllImport("kernel32.dll")]     private static extern bool AttachConsole(int pid);      [DllImport("kernel32.dll", SetLastError = true)]     private static extern bool FreeConsole();      [STAThread]     static void Main(string[] args)     {         if (args.Length > 0 && (args[0].Equals("/?") || args[0].Equals("/help", StringComparison.OrdinalIgnoreCase)))         {             // get console output             if (!AttachConsole(-1))                 AllocConsole();              ShowHelp(); // show help output with Console.WriteLine             FreeConsole(); // detach console              // get command prompt back             System.Windows.Forms.SendKeys.SendWait("{ENTER}");               return;         }          // normal winforms code         Application.EnableVisualStyles();         Application.SetCompatibleTextRenderingDefault(false);         Application.Run(new MainForm());      } 
like image 23
Mike Fuchs Avatar answered Sep 17 '22 11:09

Mike Fuchs