我正在尝试读取 C# 应用程序中的用户参数。我知道如何根据位置阅读它们
string[] args = Environment.GetCommandLineArgs();
但我想从 switch 中读取它们,例如
app.exe /f /d:foo
我真的很难找到关于这样做的任何信息......
最佳答案
你为什么不解析传递的参数数组并根据它们采取行动,就像这样
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;
}
}
关于c# - 读取命令行开关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9742924/