Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Is it possible to create a Windows Forms application that can run from the command line with parameters?

I would like a Windows Forms application that will contain a UI, but I want it to run from the command line with some parameters, possibly also a /hide or /visible=false option.

How is it possible to read in the command line parameters? And adjust accordingly?

like image 229
JL. Avatar asked Oct 05 '09 18:10

JL.


2 Answers

If you change this default Main signature:

[STAThread]
static void Main()

To this:

[STAThread]
static void Main(String[] args)

You can access the commandline variables as you would from a normal console app, or if you want to access them from elsewhere you can use:

System.Environment.GetCommandLineArgs();
like image 177
Steven Robbins Avatar answered Oct 21 '22 19:10

Steven Robbins


[STAThread]
static void Main(string[] args)
{
    if (args.Length == 0)
    {
        // Run the application in a windows form
        Application.Run(new MainForm( ));
    }
    else
    {
        // Run app from CLI
        Console.WriteLine(DoStuff(args));
    }
}
like image 30
Dour High Arch Avatar answered Oct 21 '22 18:10

Dour High Arch