Reputation: 1853
I need to write something that will get the start-up arguments and then do something for those start-up args, and I was thinking that switch would be good but it only accepts for ints and it has to be for a string
This isn't the actual code but I want to know how to make something like this work
namespace Simtho
{
class Program
{
static void Main(string[] args)
{
switch (Environment.GetCommandLineArgs())
{
case "-i":
Console.WriteLine("Command Executed Successfully");
Console.Read;
break;
}
}
}
}
Upvotes: 4
Views: 3387
Reputation: 14923
Environment.GetCommandLineArgs() returns an array of strings. Arrays cannot be switched on. Try iterating over the members of the array, like this:
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;
}
}
}
}
}
Upvotes: 8
Reputation: 21882
Environment.GetCommandLineArgs() returns a string[]
You can't switch on a string array. You probably want to test if the array contains certain values though.
Upvotes: 0
Reputation: 2524
Environment.GetCommandLineArgs() returns array of strings?
And maybe i'm wrong but internally switch on strings converted to if-else sequence...
Upvotes: 0
Reputation:
What about something like this?
string[] args = Environment.GetCommandLineArgs();
if (args.Contains("-i"))
{
// Do something
}
Upvotes: 5