1

I wrote a small application to copy files from source to destination. I expected two parameters, source and destination. The following command works fine.

test.exe "C:\source path" "D:\destination"

The problem I have is when I pass the parameter within " and end it with \ then it messes up.

For example:

test.exe "C:\source path\" "D:\destination"

Since \ is an escape character then test.exe gets the first argument as C:\source path" instead of C:\source path\

So, what's the proper way to deal with this problem.

Even the most basic sample shows that strange behavior - \" passed from command line get converted into ":

static void Main(string[] args)
{
 foreach(var s in args)
 {
    System.Console.WriteLine(s);
 }
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Anonymous
  • 9,366
  • 22
  • 83
  • 133
  • 2
    `\ ` is not "escape character" for CMD, `^` is... – Alexei Levenkov Feb 06 '14 at 03:19
  • @AlexeiLevenkov thanks for comment, I have updated the title to make it clearer. – Anonymous Feb 06 '14 at 03:28
  • Turned out somewhat "well known" behavior :(. http://www.bing.com/search?q=c%23+command+line+quotes – Alexei Levenkov Feb 06 '14 at 03:42
  • @AlexeiLevenkov I read the link you marked as duplicate before I posted this question but there is no answer there, just explanation. My intention is to find the solution or work-around for this problem, not the reason why it works this way. – Anonymous Feb 06 '14 at 04:33
  • You could have linked it in the beginning (and phrase your question more like you comment) to save some confusion in answers... But I'm afraid it would be off-topic as there really 2 options - find parser you like (off-topic as searching for links) or write your own (need code in the post, I'd probably search for existing instead). – Alexei Levenkov Feb 06 '14 at 04:40
  • @AlexeiLevenkov It's possible to write a parser to solved this problem or finding an existing one but this is such a common problem and I think it might have a best practice or a proper way to deal it. There are many console apps out there that deal with `\` properly and I am pretty sure that that is a way to do this nicely. Again, the point is this question is not duplicate with the one above. There is no answer on that one. – Anonymous Feb 06 '14 at 05:00
  • You probably should edit your question so it is clear what you are looking for and why it would not be duplicate of something like "related" - http://stackoverflow.com/questions/491595/best-way-to-parse-command-line-arguments-in-c . – Alexei Levenkov Feb 06 '14 at 05:11

1 Answers1

0
 test.exe @"C:\source path\" @"D:\destination"

The @ makes the string a verbatim literal.

In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence.

Read more here on msdn

user2140261
  • 7,855
  • 7
  • 32
  • 45
  • @alexeilevenkov updated answer with a link from msdn – user2140261 Feb 06 '14 at 03:36
  • 1
    Indeed if it would be C# code, not command prompt. Have you tried to use it from command line? You'll get `@c:\source path"`, not `c:\source path\` as argument – Alexei Levenkov Feb 06 '14 at 03:38
  • @alexeilevenkov I apologize if I am wrong, but the question title says ".NET Console Application" not command line or batch? – user2140261 Feb 06 '14 at 03:54
  • May be confusing... But if you read question carefully (even before edits) - "The following command works fine..." - explains at least to me that problem with command line arguments from CMD, not any other way of invoking the tool. (BTW I think your sample is somewhat strange - with `Process.Start` it would make more sense). – Alexei Levenkov Feb 06 '14 at 03:57