简体   繁体   English

命令行 arguments 数组

[英]command line arguments array

I am having a command line arguments as like this I need to get the two as like this how is it possible我有一个像这样的命令行 arguments 我需要像这样得到两个怎么可能

ApplicationId =1;应用程序Id = 1; Name =2姓名=2

I like to get the two values 1,2 in single array how to do this.我喜欢在单个数组中获取两个值 1,2 如何执行此操作。

It isn't entirely clear to me, but I'm going to assume that the arguments are actually:我不是很清楚,但我假设 arguments 实际上是:

 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:由于系统如何拆分 arguments,间距等很重要。在Main(string[] args)方法中,这将是一个长度为 2 的数组。您可以处理它,例如将其处理成字典:

    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);
    }

There's some good libraries mentioned on 631410 and 491595 . 631410491595中提到了一些不错的库。 I've personally used the WPF TestAPI Library mentioned by sixlettervariables and it is indeed pretty darn good我个人使用过 sixlettervariables 提到的WPF TestAPI 库,它确实非常好

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你当然需要更好的错误检查,但是 value1 和 value2 应该分别是“1”和“2”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM