Get Command Line Arguments in a Windows Forms Application

By Lowell Heddings on September 22nd, 2006

A useful technique for your applications is allowing them to parse command line arguments. This can give a lot of extra functionality to your application, for instance to pass the name of a file to open on the command line.

Most of the examples you’ll find online will show you something like this:

static void Main(string[] args)
{
    foreach(string arg in args)
    {
       Console.WriteLine(arg);
    }
   Console.ReadLine();
}

That’s all fine and good, except that won’t work for our Windows Forms application without changing the type of the project to console, etc.

Thankfully, this is completely unnecessary, because you can simply do this:

string[] args = Environment.GetCommandLineArgs();

foreach(string arg in args){
// do stuff
}

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.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 09/22/06
More Articles You Might Like

Enter Your Email Here to Get Access for Free:

Go check your email!