CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Thursday, September 14, 2006

.NET Command Line Confusion

I kept getting wierd errors in a simple console application which takes a regular expression as an argument. The regular expression kept failing with an "Illegal \ at end of pattern" error. The odd thing was that I was properly escaping the \ as \\.

After some testing, I think I've convinced myself that this had nothing to do with regular expressions; there's something wacky about the way the .NET Framework handles commandline arguments which end in \\". Here's a simplified argument test application: using System;

public class Test
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Environment.CommandLine: " + Environment.CommandLine);
        for(int i=0;i<args.Length;i++)
        {
            OutputArg(i,args[i]);
            Console.WriteLine();
        }
    }
   
    static void OutputArg(int i,string arg)
    {
        Console.WriteLine("Environment.GetCommandLineArgs()[{0}]: {1}", i+1, Environment.GetCommandLineArgs()[i+1]);
        Console.WriteLine("args[{0}]: {1}",i,arg);
        //foreach(char c in arg) Console.WriteLine(c);
    }
}

Continue the Tutorial

 

Post a Comment