Unrecognized escape sequence for path string containing backslashes

Kjensen picture Kjensen · Aug 20, 2009 · Viewed 239.1k times · Source

The following code generates a compiler error about an "unrecognized escape sequence" for each backslash:

string foo = "D:\Projects\Some\Kind\Of\Pathproblem\wuhoo.xml";

I guess I need to escape backslash? How do I do that?

Answer

Brandon picture Brandon · Aug 20, 2009

You can either use a double backslash each time

string foo = "D:\\Projects\\Some\\Kind\\Of\\Pathproblem\\wuhoo.xml";

or use the @ symbol

string foo = @"D:\Projects\Some\Kind\Of\Pathproblem\wuhoo.xml";