C# 读取命令行开关

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9742924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 08:39:54  来源:igfitidea点击:

read command line switch

c#.net

提问by MTeck

I'm trying to read user arguments in a C# application. I know how to read them based on position with

我正在尝试在 C# 应用程序中读取用户参数。我知道如何根据位置阅读它们

string[] args = Environment.GetCommandLineArgs();

but I'd like to read them from switches such as

但我想从开关中读取它们,例如

app.exe /f /d:foo

I'm really struggling to find any information on doing this...

我真的很难找到关于这样做的任何信息......

采纳答案by Jason

Why don't you just parse the array of arguments passed and act based on them, like this

你为什么不只是解析传递的参数数组并根据它们采取行动,就像这样

foreach (string arg in args)
{
    switch (arg.Substring(0, 2).ToUpper())
    {
        case "/F":
            // process argument...
            break;
        case "/Z":
            // process arg...
            break;
        case "/D":
            paramD = arg.Substring(3);
            break;
        default:
            // do other stuff...
            break;
    }
}

回答by Ed S.

void Main(string[] args )
{
    foreach( var arg in args )
    {
        // process each arg if needed, i.e., 
        // remove '-', '/', uppercase, whatever
        switch(arg)
        {
            case "blah":
            // ...
        }
    }
}

Libraries do exist for this purpose which make the whole process a lot easier, but if this is a one off app it may not be worth it. Depends on how complex your command line arguments are.

为此目的确实存在图书馆,这使整个过程变得更加容易,但如果这是一个一次性的应用程序,它可能不值得。取决于您的命令行参数的复杂程度。

回答by Tigran

Well, basically you already done. Process that string[]array you get from the framework and you done. There is no built-in way to achieve what you're asking for.

嗯,基本上你已经完成了。处理string[]您从框架中获得的数组,然后就完成了。没有内置的方法可以实现您的要求。

Like 3rd parties solution can have a look on

像第3方解决方案可以看看

C#/.NET Command Line Arguments Parser

C#/.NET 命令行参数解析器

回答by Bernard

I am the author of an open source .NET command-line library that may suit your needs: C# CLI.

我是一个可以满足您需求的开源 .NET 命令行库的作者:C# CLI

You may also want to read thisquestion for other suggestions.

您可能还想阅读问题以获取其他建议。

回答by sfuqua

I believe you'll have to roll your own implementation. After writing unit tests to evaluate the new method, I might start thinking along the lines of ...

我相信你必须推出自己的实现。在编写单元测试来评估新方法之后,我可能会开始思考......

foreach (string arg in args)
{
    flagF = flagF || arg == "/f"; // assuming F is boolean
    flagD = flagD ?? (arg.Substring(0,3) =="/d:" ? arg.Substring(3) : null);
}

回答by Chuck Savage

What about,

关于什么,

// first is exe of executing program
string[] args = Environment.CommandLine.Split('/').Skip(1).ToArray();
foreach (string arg in args)
{
    string value = arg.Trim();
    switch (value)
    {
        case "f":
            //...
            continue;
    }
    if (value.StartsWith("d:"))
    {
        value = value.Substring(2);
        // ...
        continue;
    }
}

回答by Stephen Huff

I've seen a couple of answers that loop through the args string collection - the problem is you need the next item after you hit the switch you are looking for. I used the array index of the collection in order to get the next item.

我已经看到了几个循环遍历 args 字符串集合的答案 - 问题是您在按下要查找的开关后需要下一个项目。我使用集合的数组索引来获取下一个项目。

In my sample I'm looking for a server and a port /S and /P. I hope this helps someone:

在我的示例中,我正在寻找服务器和端口 /S 和 /P。我希望这可以帮助别人:

        var server = string.Empty;
        var port = string.Empty;

        for(var x = 0; x < args.Count(); x++)
        {
            switch (args[x].Trim().ToUpper())
            {
                case "/S":
                    server = args[x + 1];
                    break;
                case "/P":
                    port = args[x + 1];
                    break;                                        
            }
        }