读命令行开关 [英] read command line switch

查看:159
本文介绍了读命令行开关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读取用户的参数在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;
    }
}

这篇关于读命令行开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆