首页 > 解决方案 > 传递参数不必担心命令exe c#

问题描述

尝试传递参数而不必考虑参数的顺序。第一行是我想要做的,下面是我能做的。

我想要什么都应该给出相同的结果

test.exe -word1 hello -word2 bye
test.exe -word2 bye -word1 hello

我有的

test.exe hello bye

static void Main(string[] args)
        {
            console.line(args[0])
            console.line(args[1])
        }

标签: c#.netparametersexe

解决方案


您可以像这样使用字符串输入:

test.exe -word1=hello -word2=bye
test.exe -word2=bye -word1=hello

然后你可以做这样的事情:

static void Main(string[] args)
        {
            // Single if the param is needed, else SingleOrDefault (to get null if the param. was not found
            var word1 = args.Single(c => c.startsWith("-"+nameof(word1))
                            .Split(new char[] {'='})
                            .Last();
            var word2 = args.Single(c => c.startsWith("-"+nameof(word2))
                            .Split(new char[] {'='})
                            .Last();                            
        }

这是伪代码,我没有运行它 - 只是为了给你一个例子。

我什至不会打扰,只是拍拍:https ://github.com/Tyrrrz/CliFx


推荐阅读