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

前端 未结 4 772
走了就别回头了
走了就别回头了 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:15

    if you have a "=" in your parameter values, a safer option would be:

    private static Dictionary ResolveArguments(string[] args)
    {
        if (args == null)
            return null;
    
        if (args.Length > 1)
        {
            var arguments = new Dictionary();
            foreach (string argument in args)
            {
                int idx = argument.IndexOf('=');
                if (idx > 0)
                    arguments[argument.Substring(0, idx)] = argument.Substring(idx+1);
            }
            return arguments;
        }
    
        return null;
    }
    

提交回复
热议问题