0

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.

1
  • You could probably help us by clarifying the question... it isn't 100% clear what the args look like or what you want to do Commented Jun 16, 2009 at 8:07

3 Answers 3

6

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);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that's pretty much what I do for utilities that require named arguments.
2

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

Comments

1

Try

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.