locked
Command Line Arguments RRS feed

  • Question

  • I'm writing a console program in C#. I'm attempting to see if the user sets a command line argument using code like this:

    static void Main(string[] args)
    {
        if ( args[0] == "nogui")
        {
           //start in quiet mode
        }
    }

    The only problem with the above approach is  if there isn't a command line argument, it throws an exception because it exceeds the boundry of args. Does anyone know of a better way to check for command line arguments?
    Tuesday, September 19, 2006 6:45 PM

Answers

  • Try:

    static void Main(string[] args)
    {
        switch(args.Length)
           {
              case 0: // code for no arguments
                       break;
             case 1: // code for one argument
                       break;
              case 2: //code for two argument
                       break;
              // etc.. etc.. etc..
              default: //code for unrecognized number of arguments
                       break;
            }
    }

    Regards.
    Tuesday, September 19, 2006 7:24 PM

All replies

  • Try:

    static void Main(string[] args)
    {
        switch(args.Length)
           {
              case 0: // code for no arguments
                       break;
             case 1: // code for one argument
                       break;
              case 2: //code for two argument
                       break;
              // etc.. etc.. etc..
              default: //code for unrecognized number of arguments
                       break;
            }
    }

    Regards.
    Tuesday, September 19, 2006 7:24 PM
  • Thanks!
    Tuesday, September 19, 2006 7:56 PM