.Net: Example basic consoleapplication with parameters

This is a basic example of an console-application with parameters used for a scheduled tasks.

The application will be scheduled to run every day with the -n parameter. But the application can also run on a specific date by using the -d parameter.


class Program
{
 //Parameters
 private static DateTime theDate;
 private static RunMode mode = RunMode.NotSpecified;

 static void Main(string[] args)
 {
  if (CollectParameters(args))
  {
   Console.WriteLine("Beginning appliation in mode "{0}" with date {1}.", mode, theDate);
   Logger.LogEvent("Starting StatisticConsoleApp", args, (int)Logger.LogType.Message);

  }
 }

 private static bool CollectParameters(string[] args)
 {
  if (args.Length > 0)
  {
   for (int ii = 0; ii < args.Length && mode == RunMode.NotSpecified; ii++)
   {
    //Default commands for support -h and /?
    if (args[ii].ToLower().Trim() == "-h" || args[ii].Trim() == "/?")
    {
     PrintHelpText();
     return (false);
    }
    else
    {
     //Get clear input params
     string inputParam = args[ii].ToLower().Trim();
     inputParam = Regex.Replace(inputParam, "[/-]", "");

     switch (inputParam)
                    {
                        case "n":
                            mode = RunMode.Normal;
                            theDate = DateTime.Now;
                            break;

                        case "d":
                            mode = RunMode.SpecificDate;
                            if ((ii + 1) < args.Length)
                            {
                                theDate = Convert.ToDateTime(args[ii + 1]);
                            }
                            break;
       default:
        if(args[ii].Substring(0,1) == "-")
         Console.WriteLine("Unknown switch {0}. Ignoring this switch.", args[ii]);
        break;
      }
    }
   }

   if (mode != RunMode.NotSpecified)
   {
    return true;
   }
   else
   {
    Console.WriteLine("No valid parameter specified. Aborting processing.....");
    return false;
   }
  }
  else
  {
   PrintHelpText();
   return false;
  }
 }
 private static void PrintHelpText()
 {
  Console.WriteLine("nUsage: Statisticprocess application");
  Console.WriteLine("");
  Console.WriteLine("  -n");
  Console.WriteLine("    The application will run in normal mode.");
  Console.WriteLine("    The statistics will be processed with the current date.");
  Console.WriteLine("  -d");
  Console.WriteLine("    The application runs in 'specific date' mode.");
  Console.WriteLine("    The statistics will be processed with the input date");
  Console.WriteLine("    The date may be specified in any format as long as it can be ");
  Console.WriteLine("    translated unambiguously into a proper date.");
  Console.WriteLine("");
  Console.WriteLine("");
  Console.WriteLine("    Note: Only one mode can be specified.");
  Console.WriteLine("          If two modes are specified, the second mode is ignored.");
  Console.WriteLine("");
  Console.WriteLine("    Example: In this example application runs in "specific date" mode ");
  Console.WriteLine("             for the date "10-sep-2006".");
  Console.WriteLine("");
  Console.WriteLine("    TOOLNAME [-d 1-jan-2009]");
  Console.WriteLine("");
 }
}


If you copy this class into your program.cs you will get some errors concerning my own loghandler and the missing RunMode enum. To fix this delete the code lines for the logging and add the following enum to your console application.



public enum RunMode : byte
 {
  NotSpecified,
  Normal,
  SpecificDate
 }


This is not the clearest example but if you have any questions don’t hesitate to ask.

2 Replies to “.Net: Example basic consoleapplication with parameters”

Leave a Reply

Your email address will not be published. Required fields are marked *