读命令行开关

读命令行开关

问题描述:

我想读取用户的参数在C#应用程序。我知道如何根据使用位置阅读

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

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...

你为什么不只是分析的基础上他们传递的参数和行为的阵列,像这样

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