Even though we live in a GUI world there will always be a place for a console app. In my day to day adventures as a Database Administrator I still find myself heavily dependent on batch files for automating processes. Sometimes, when the logic is a little bit to complex for dos commands to handle I will create a new console app in visual studio. Let's face it, if statements and for loops in C# are much easier on the eye when compare to the dos way of doing things.

Tip #1. - How to display debug information only when the project is running from Visual Studio.

The trick here is to use the System.Diagnostics.Debugger.IsAttached property. You could create an extension method or a normal method to handle your logging. For example:

   1:  static void DebugLog(string s)
   2:  {
   3:     if (System.Diagnostics.Debugger.IsAttached)
   4:        Console.WriteLine(s);
   5:  }

This is handy if you want to see diagnostic information during debugging but you want to hide it when you run it in production. The TraceWriter class does a similar thing but my technique is quick and dirty and it definitely gets the job done.

Tip #2. - (Newbie level) Sometimes when you run a console app from visual studio the application run so fast and closes that you never see the output.

Ahhh...this is an easy fix. Just add the statement Console.Readline() as the last line in your program. This will keep the window open until you hit the enter key.

Tip #3. - How do I parse out all of the command line arguments?

Regular expressions is the answer. I basically use this block of code for parsing my command line arguments. This code block relies on you using an argument pattern similar to osql or sqlcmd. For example, to connect to a server named mysqlserver with windows authentication in osql you would issue the following command:

osql -S mysqlserver -E

The -S argument is for the servername and the -E argument means that you want to use windows authentication.


   1:  static void ProcessArguments(string[] args)
   2:  {
   3:     string argString = String.Empty;
   4:     //do a little work to remove unecessary spaces between the option and value
   5:     //add commas in order to make it easier to split the statements
   6:     foreach (string s in args) {
   7:        if (s.StartsWith("-") && argString.Length > 0)
   8:           argString += ",";
   9:   
  10:        argString += s;
  11:     }
  12:   
  13:     //split the arguments up by using the commas we just added
  14:     string[] fixedArgs = argString.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
  15:   
  16:     string pattern = @"-(?<option>[a-zA-Z])(?<value>.+)#";
  17:     foreach (string argument in fixedArgs) {
  18:        Match match = Regex.Match(argument, pattern, RegexOptions.IgnorePatternWhitespace);
  19:        if (match != null) {
  20:           //split the options into the key, value pair and set the appropriate static variable
  21:           string option = match.Groups["option"].Value.ToUpper();
  22:           string value = match.Groups["value"].Value;
  23:   
  24:           //Console.WriteLine( "{0}: {1}", option, value );
  25:           if (option == "D") {
  26:              _directory = value;
  27:           }
  28:           else if (option == "H") {
  29:              _headerFile = value;
  30:           }
  31:        }
  32:     }
  33:  }

On a related note. If you are doing a lot of work with regular expressions you may be interested in the Regular Expression Designer from rad software. I found an article about it a few months ago in a magazine and it has been on my workstation ever since. Basically the tool allows you to learn, develop and test Regular expressions. Best of all, it is a free download.