Several arguments in a console application are not parsed correctly

Easy to reproduce but very weird for me:

Add the following 3-line arguments to the command-line arguments of the Text Box in VisualStudio (under Project Properties> Debugging-> Startup Options):

-SourceFile:"c:\temp\file.txt" -DestinationFolder:"c:\temp\" -ArchiveFolder:"C:\temp\" 

Test it with this simple console application:

 class Program { static void Main(string[] args) { foreach (string t in args) { Console.WriteLine(t); } Console.ReadKey(); } } 

Result: an array (args []) has 2 instead of 3 lines?

 [0] SourceFile:c:\temp\file.txt [1] DestinationFolder:c:\temp" -ArchiveFolder:C:\temp" 

Can someone explain to me why this is happening? There is something strange in that the quotes cause normal, the quotes will be removed .net, but there are still some quotes ... but I do not see the problem ...

Thanks for any help!

+8
c # command-line-arguments console console-application
source share
1 answer

You have a \" in the DestinationFolder value that" speeds up the quote, including it in the value text, instead of associating it with an opening quote to close the line. You want a literal, \ , so use \\ :

 -SourceFile:"c:\temp\file.txt" -DestinationFolder:"c:\temp\\" -ArchiveFolder:"C:\temp\\" 

(you can even see the escaping in action in the SO backlight engine)

+12
source share

All Articles