[RESOLVED] How To Pass A Startup Parameter To A Windows C# Program
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...
Re: How To Pass A Startup Parameter To A Windows C# Program
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.
Code:
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.
}
}
Re: How To Pass A Startup Parameter To A Windows C# Program
Re: How To Pass A Startup Parameter To A Windows C# Program
Don't forget to resolve your thread from the Thread Tools menu.
Re: [RESOLVED] How To Pass A Startup Parameter To A Windows C# Program
Re: [RESOLVED] How To Pass A Startup Parameter To A Windows C# Program
Quote:
Originally Posted by 2MuchRiceMakesMeSick
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:
Code:
public string myProgramArgs;
static void Main(string[] args)
{
myProgramArgs = args;
Application.Run(new Form1);
}