views:

754

answers:

3

In c#.Net I am not able to fetch the commandline argument properly. It has problems in case i give the command like:

myProgram.exe "c:\inputfolder\" "d:\output.txt"

due to the backslash character(which i think acting as an escape character) in the args[] array i am getting only one argument instead of two It works fine if i gave without backslash:

myProgram.exe "c:\inputfolder" "d:\output.txt"

or without double quotes:

myProgram.exe c:\inputfolder\ "d:\output.txt"
+1  A: 

This is a well known parsing problem and there isn't a whole lot you can do about it besides get the whole command line as a single string and parse it yourself.

Peter Wone
Thanks Peter...
+2  A: 

I've never experienced such a problem but in case you like to parse the command line by your self use System.Environment.CommandLine to get it.

Chris Richner
+3  A: 

The backslash is escaping the quote character in the shell. You have to use an extra backslash:

myProgram.exe "c:\inputfolder\\" "d:\output.txt"

You can use the following short sample program to test command line parsing:

using System;

class Program
{
    static void Main(string[] args)
    {
        for (int i = 0; i < args.Length; i++)
        {
            Console.WriteLine(string.Format("Argument {0}: {1}", i, args[i]));
        }
    }
}
0xA3
Thakns for reply ...but this wount help meUser can give the input as myProgram.exe "c:\inputfolder\" "d:\output.txt"I think anyhow i have to parse it.
I would suggest you print a reasonable error message in that case instead of handling the incorrect input. \" is the way the shell uses to pass a quote character to an application, you should not break that default behavior.
0xA3
hi Divo, i think you have mistaken me.I am using "visual studio 2008" for writing my c# appliction. I ve wrote program as suggested by you: static void Main(string[] args) { for (int i = 0; i < args.Length; i++) { Console.WriteLine(string.Format("Argument {0}: {1}", i, args[i])); } }.....Input for the above program is - myProgram.exe "c:\inputfolder" "d:\output.txt"In the debug mode, when i put my cursor on the args[] array, it shows that the array contains only one item - args[0] = "c:\inputfolder\" d:\output.txt"
Adding one more thing.... actually i need to separate further Validation of valid path... and it fails of course, because - (1). i am not getting the separate path (2). the value i am getting "c:\inputfolder\" d:\output.txt" has a double quote, hence fails against path validation...I think this makes it more clear
the same problem occurs using the commandline arguments field in the project properties of visual studio 2005; +1 for the fix
Steven A. Lowe