Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run application as Console app with args passed, otherwise run as Win Form app

I have an application, that I want to run like this:

  • if args sent - I want it to behave as console application

  • if args not sent - I want it to run as windows forms application

When I set output type to console and when args not detected I'm enabling visual styles and running. It works great, except the console window opens/closes quickly and that really bothers me. Is there a way to not have the console window appear?I'm looking for a way to not create it at all.

like image 452
eddyuk Avatar asked Jan 11 '12 14:01

eddyuk


2 Answers

I'd do it the other way round, make it a WinForms app, because you get into code before anything is created (in your main function in program.cs).

See this link: http://www.rootsilver.com/2007/08/how-to-create-a-consolewindow

like image 116
George Duckett Avatar answered Oct 04 '22 01:10

George Duckett


  1. Create a Windows.Forms application
  2. Add an external function

    [System.Runtime.InteropServices.DllImport( "kernel32.dll" )]
    private static extern bool AllocConsole();
    
  3. In the main method of your application:

    if ( windows_app )
    {
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1());
    }
    else
    { 
        AllocConsole();
        Console.WriteLine( "foo" );
    }
    
like image 30
Wiktor Zychla Avatar answered Oct 03 '22 23:10

Wiktor Zychla