我需要写一些东西来获取启动参数,然后为这些启动参数做一些事情,我认为 switch 会很好,但它只接受整数而且必须是字符串
这不是实际的代码,但我想知道如何实现这样的功能
namespace Simtho
{
class Program
{
static void Main(string[] args)
{
switch (Environment.GetCommandLineArgs())
{
case "-i":
Console.WriteLine("Command Executed Successfully");
Console.Read;
break;
}
}
}
}
最佳答案
Environment.GetCommandLineArgs() 返回一个字符串数组。无法打开阵列。尝试遍历数组的成员,如下所示:
namespace Simtho
{
class Program
{
static void Main(string[] args)
{
foreach (string arg in Environment.GetCommandLineArgs())
{
switch (arg)
{
case "-i":
Console.WriteLine("Command Executed Successfully");
Console.Read();
break;
}
}
}
}
}
关于c# - 带字符串的 Switch 语句 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1735439/