How do I pass command-line arguments to a WinForms application?

I have two different WinForms applications, AppA & AppB. Both are running .NET 2.0. In AppA I want to open AppB, but I need to pass command-line arguments to it. How do I consume the arguments that I pass in the command line? This is my current main method in AppB, but I don't think you can change this?

 static void main()
1,420 2 2 gold badges 12 12 silver badges 11 11 bronze badges asked Jul 24, 2009 at 19:00 MRFerocius MRFerocius 5,619 8 8 gold badges 40 40 silver badges 47 47 bronze badges

6 Answers 6

The best way to work with args for your winforms app is to use

string[] args = Environment.GetCommandLineArgs(); 

You can probably couple this with the use of an enum to solidify the use of the array througout your code base.

"And you can use this anywhere in your application, you aren’t just restricted to using it in the main() method like in a console application."

57k 24 24 gold badges 105 105 silver badges 109 109 bronze badges answered Aug 26, 2013 at 16:43 Hannibul33 Hannibul33 2,471 1 1 gold badge 14 14 silver badges 4 4 bronze badges

The first element in the array contains the file name of the executing program. If the file name is not available, the first element is equal to String.Empty. The remaining elements contain any additional tokens entered on the command line.

Commented Apr 2, 2014 at 7:15

@docesam That helped me a lot, thanks! Was wondering why it kept trying to load the program itself as text.

Commented Aug 29, 2015 at 5:01 Commented Nov 22, 2016 at 15:49 After years of C# development i never knew this method existed. Nice. Commented May 25, 2018 at 16:41 is there a benefit to using this method vs sending the parameters main(string[] args) ? Commented Jun 14, 2018 at 14:09
static void Main(string[] args) < // For the sake of this example, we're just printing the arguments to the console. for (int i = 0; i < args.Length; i++) < Console.WriteLine("args[] == ", i, args[i]); > > 

The arguments will then be stored in the args string array:

$ AppB.exe firstArg secondArg thirdArg args[0] == firstArg args[1] == secondArg args[2] == thirdArg 
answered Jul 24, 2009 at 19:02 180k 55 55 gold badges 372 372 silver badges 495 495 bronze badges

input: "whatever.exe -v foo /lol nisp". Output: args[0] = "-v"; args[1] = "foo"; args[2] = "/lol"; args[3] = "nisp"; What could be easier?

Commented Jul 24, 2009 at 19:22

cant believe i saw that 'string[] args' so many times after a whole year and it never occured to me wtf it was untill now ! haha

Commented Jun 28, 2016 at 18:31

It seems that args[0] is the full path and exe name of the application running and args[1] is the first parameter.

Commented Apr 21, 2020 at 0:09

The question is about WinForms applications -- which have no Console and user functionality needs to run from the main form.

Commented Nov 2, 2022 at 10:09 @ivan_pozdeev Console is just for the example of course, as commented in the code. Commented Nov 2, 2022 at 12:50

Consider you need to develop a program through which you need to pass two arguments. First of all, you need to open Program.cs class and add arguments in the Main method as like below and pass these arguments to the constructor of the Windows form.

static class Program < [STAThread] static void Main(string[] args) < Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(args[0], Convert.ToInt32(args[1]))); >> 

In windows form class, add a parameterized constructor which accepts the input values from Program class as like below.

public Form1(string s, int i) < if (s != null && i >0) MessageBox.Show(s + " " + i); > 

To test this, you can open command prompt and go to the location where this exe is placed. Give the file name then parmeter1 parameter2. For example, see below

C:\MyApplication>Yourexename p10 5 

From the C# code above, it will prompt a Messagebox with value p10 5 .

answered May 26, 2016 at 9:57 Sarath Subramanian Sarath Subramanian 21.1k 11 11 gold badges 87 87 silver badges 92 92 bronze badges

You can grab the command line of any .Net application by accessing the Environment.CommandLine property. It will have the command line as a single string but parsing out the data you are looking for shouldn't be terribly difficult.

Having an empty Main method will not affect this property or the ability of another program to add a command line parameter.

answered Jul 24, 2009 at 19:02 750k 151 151 gold badges 1.3k 1.3k silver badges 1.5k 1.5k bronze badges

Or use Environment.GetCommandLineArgs() which returns an string array of arguments just like Main(string[] args)

Commented Jun 11, 2010 at 3:49

You use this signature: (in c#) static void Main(string[] args)

This article may help to explain the role of the main function in programming as well: http://en.wikipedia.org/wiki/Main_function_(programming)

Here is a little example for you:

class Program < static void Main(string[] args) < bool doSomething = false; if (args.Length >0 && args[0].Equals("doSomething")) doSomething = true; if (doSomething) Console.WriteLine("Commandline parameter called"); > > 
8,558 1 1 gold badge 41 41 silver badges 58 58 bronze badges answered Jul 24, 2009 at 19:09 chrisghardwick chrisghardwick 333 1 1 silver badge 5 5 bronze badges

This may not be a popular solution for everyone, but I like the Application Framework in Visual Basic, even when using C#.

Add a reference to Microsoft.VisualBasic

Create a class called WindowsFormsApplication

public class WindowsFormsApplication : WindowsFormsApplicationBase < /// /// Runs the specified mainForm in this application context. /// /// Form that is run. public virtual void Run(Form mainForm) < // set up the main form. this.MainForm = mainForm; // Example code ((Form1)mainForm).FileName = this.CommandLineArgs[0]; // then, run the the main form. this.Run(this.CommandLineArgs); >/// /// Runs this.MainForm in this application context. Converts the command /// line arguments correctly for the base this.Run method. /// /// Command line collection. private void Run(ReadOnlyCollection commandLineArgs) < // convert the Collectionto string[], so that it can be used // in the Run method. ArrayList list = new ArrayList(commandLineArgs); string[] commandLine = (string[])list.ToArray(typeof(string)); this.Run(commandLine); > > 

Modify your Main() routine to look like this

static class Program < [STAThread] static void Main() < Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var application = new WindowsFormsApplication(); application.Run(new Form1()); >> 

This method offers some additional usefull features (like SplashScreen support and some usefull events)

public event NetworkAvailableEventHandler NetworkAvailabilityChanged;d. public event ShutdownEventHandler Shutdown; public event StartupEventHandler Startup; public event StartupNextInstanceEventHandler StartupNextInstance; public event UnhandledExceptionEventHandler UnhandledException;