PDA

Click to See Complete Forum and Search --> : [RESOLVED] How To Pass A Startup Parameter To A Windows C# Program


2MuchRiceMakesMeSick
Jan 13th, 2006, 11:49 PM
Anyone know how to pass a startup parameter to a program in C#.
I searched the net all over and the only thing I could find was for console applications. I also looked for overloads for the Form1_Load() and didnt find anything.

I would like to pass options to my program such as

program.exe /a
program.exe /b
program.exe /c
etc...

jmcilhinney
Jan 14th, 2006, 12:21 AM
You pass them exactly has you have shown when starting the program. To read them within your app you would use the Environment.GetCommanLineArgs method.string[] args = Environment.GetCommandLineArgs();

// The first commandline argument is always the executable path itself.
if (args.Length > 1)
{
if (Array.IndexOf(args, "/a") != -1)
{
// The "/a" switch was used.
}

if (Array.IndexOf(args, "/b") != -1)
{
// The "/b" switch was used.
}

if (Array.IndexOf(args, "/c") != -1)
{
// The "/c" switch was used.
}
}

2MuchRiceMakesMeSick
Jan 14th, 2006, 03:42 AM
cool thanks alot

jmcilhinney
Jan 14th, 2006, 05:53 AM
Don't forget to resolve your thread from the Thread Tools menu.

2MuchRiceMakesMeSick
Jan 14th, 2006, 01:49 PM
done. thanks again

tr333
Jan 14th, 2006, 05:50 PM
I also looked for overloads for the Form1_Load() and didnt find anything.
If you start the program with the "Main" function instead of Form1_Load, you can get the command line args from there:


public string myProgramArgs;

static void Main(string[] args)
{
myProgramArgs = args;
Application.Run(new Form1);
}