I'm looking for a way that I can parse command line arguments into my WPF application with just a way of reading the value of the argument that the user passed.
As an example
application.exe /setTime 5
is there a way for me to have some code where I can just say:
MessageBox.Show(arg("setTime"));
Which will output 5
Working Solution
When you parse the command line put the argument/value pairs in a
Dictionary
with the argument as the key. Then yourarg("SetTime")
will become:(Obviously you don't want the actual dictionary to be public.)
To get the arguments in the first place you can use:
This will return all the arguments so you will need to parse the array in steps of two (after first checking that the length is a multiple of two + 1):
The first element of the array is the name of the executing program - MSDN Page - so your loop needs to start from one:
This loops in steps of two as you define each argument is a pair of values: the identifier and the actual value itself, e.g.
Then you can simply see if the argument is specified by seeing if the key
-arg1
is in the dictionary and then read it's value:This means you can have the arguments in any order and omit any arguments you don't want to specify.