Question : c# .net - get command line argument on form load

Hi,
I have a c# .net winforms application.

I want to start the program from command line like this -

   program.exe stringtext

and then be able to use "stringtext" within Form1_load.

However, the program will not always be started like this. So it needs to be optional.

Can someone point me in the right direction with this? Im reading many different ways to do it and I cant get any to work.

Sm17ch

Answer : c# .net - get command line argument on form load

You can access the command line parameters by declaring your Main function like so:

public static void Main(string[] args)

You'll want to store these somewhere that your WinForm can access them from.  I would suggest a static class (this used to be called a global variable):

public static class ArgHolder
{
  public static Args { get; set; }
}

Now, from Main, you can just call ArgHolder.Args = args;

From your WinForm, you can retrieve these when you need them.

if ( ArgHolder.Args != null && ArgHolder.Args.Length > 0 )
{
  // We have strings, starting with ArgHolder.Args[0]
}

Hope this helps!
Random Solutions  
 
programming4us programming4us