tags:

views:

340

answers:

4

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

    }
}
+5  A: 

What about something like this?

string[] args = Environment.GetCommandLineArgs();

if (args.Contains("-i"))
{
    // Do something
}
Developer Art
A: 

Environment.GetCommandLineArgs() returns array of strings?

And maybe i'm wrong but internally switch on strings converted to if-else sequence...

Trickster
A: 

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.

Winston Smith
+8  A: 

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