command line arguments array

Go To


I am having a command line arguments as like this I need to get the two as like this how is it possible

ApplicationId =1; Name =2

I like to get the two values 1,2 in single array how to do this.

2009-06-16 07:55
by web dunia
You could probably help us by clarifying the question... it isn't 100% clear what the args look like or what you want to d - Marc Gravell 2009-06-16 08:07



string values = "ApplicationId =1; Name =2";
string[] pairs = values.Split(';');

string value1 = pairs[0].Split('=')[1];
string value2 = pairs[1].Split('=')[1];

You'll need better error checking of course, but value1 and value2 should be "1" and "2" respectively

2009-06-16 08:03
by Binary Worrier


It isn't entirely clear to me, but I'm going to assume that the arguments are actually:

 ApplicationId=1 Name=2

the spacing etc is important due to how the system splits arguments. In a Main(string[] args) method, that will be an array length 2. You can process this, for example into a dictionary:

    static void Main(string[] args) {
        Dictionary<string, string> options = new Dictionary<string, string>();
        foreach (string arg in args)
            string[] pieces = arg.Split('=');
            options[pieces[0]] = pieces.Length > 1 ? pieces[1] : "";

        Console.WriteLine(options["Name"]); // access by key

        // get just the values
        string[] vals = new string[options.Count];
        options.Values.CopyTo(vals, 0);
2009-06-16 08:05
by Marc Gravell
Yep, that's pretty much what I do for utilities that require named arguments - Christian Hayter 2009-06-16 08:20


There's some good libraries mentioned on 631410 and 491595. I've personally used the WPF TestAPI Library mentioned by sixlettervariables and it is indeed pretty darn good

2009-06-16 08:03
by Dan F