How can I obtain the named arguments from a console application in the form of a Dictionary?

前端 未结 4 810
走了就别回头了
走了就别回头了 2021-02-05 05:18

I have a console application called MyTool.exe

What is the simplest way to collect the named arguments passed to this console applicaiton and then to put them in a

4条回答
  •  一个人的身影
    2021-02-05 06:01

    Here's how this can be done in the most simple way:

        static void Main(string[] args)
        {
            var arguments = new Dictionary();
    
            foreach (string argument in args)
            {
                string[] splitted = argument.Split('=');
    
                if (splitted.Length == 2)
                {
                    arguments[splitted[0]] = splitted[1];
                }
            }
        }
    

    Note that:

    • Argument names are case sensitive
    • Providing the same argument name more than once does not produce an error
    • No spaces are allowed
    • One = sign must be used

提交回复
热议问题