|
-
Jan 14th, 2006, 12:49 AM
#1
Thread Starter
Lively Member
[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...
-
Jan 14th, 2006, 01:21 AM
#2
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.
}
}
-
Jan 14th, 2006, 04:42 AM
#3
Thread Starter
Lively Member
Re: How To Pass A Startup Parameter To A Windows C# Program
-
Jan 14th, 2006, 06:53 AM
#4
Re: How To Pass A Startup Parameter To A Windows C# Program
Don't forget to resolve your thread from the Thread Tools menu.
-
Jan 14th, 2006, 02:49 PM
#5
Thread Starter
Lively Member
Re: [RESOLVED] How To Pass A Startup Parameter To A Windows C# Program
-
Jan 14th, 2006, 06:50 PM
#6
Re: [RESOLVED] How To Pass A Startup Parameter To A Windows C# Program
 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);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|