Let's assume that your app has three valid command line arguments: -a and -b that also require a value and -c that doesn't. I'd be inclined to do something like this:
Code:
private void Form1_Load(object sender, EventArgs e)
{
string[] args = Environment.GetCommandLineArgs();
int index = 0;
do
{
switch (args[index])
{
case "-a":
index++;
// Call a method to process the arg at index.
break;
case "-b":
index++;
// Call a method to process the arg at index.
break;
case "-c":
// Do whatever is appropriate.
break;
default:
break;
}
index++;
} while (index < args.Length);
}
If you wanted to ensure that each argument was not processed more than once then you could add a bit of validation:
Code:
private void Form1_Load(object sender, EventArgs e)
{
string[] args = Environment.GetCommandLineArgs();
List<string> processed = new List<string>();
int index = 0;
do
{
switch (args[index])
{
case "-a":
index++;
if (index < args.Length && !processed.Contains("-a"))
{
// Call a method to process the arg at index.
processed.Add("-a");
}
break;
case "-b":
index++;
if (index < args.Length && !processed.Contains("-b"))
{
// Call a method to process the arg at index.
processed.Add("-b");
}
break;
case "-c":
if (!processed.Contains("-c"))
{
// Do whatever is appropriate.
processed.Add("-c");
}
break;
default:
break;
}
index++;
} while (index < args.Length);
}
The code may be a bit lengthy but it is clear. Note that that will also just ignore invalid arguments. You might choose to notify the user of the error instead.