string foo = "D:\Projects\Some\Kind\Of\Pathproblem\wuhoo.xml";
I get a compile error "Unrecognized escape sequence" for each backslash.
I guess I need to escape backslash - how?
string foo = "D:\Projects\Some\Kind\Of\Pathproblem\wuhoo.xml";
I get a compile error "Unrecognized escape sequence" for each backslash.
I guess I need to escape backslash - how?
try this
string foo = @"D:\Projects\Some\Kind\Of\Pathproblem\wuhoo.xml";
the problem is that in a string a \ is an escape character. By using the @ sign you tell the compiler to ignore the escape characters.
You can also get by with escaping the \
string foo = "D:\\Projects\\Some\\Kind\\Of\\Pathproblem\\wuhoo.xml";
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";
string foo = "D:\\Projects\\Some\\Kind\\Of\\Pathproblem\\wuhoo.xml";
This will work, or the previous examples will, too. @"..." means treat everything between the quote marks literally, so you can do
@"Hello
world"
To include a literal newline. I'm more old school and prefer to escape "\" with "\\"
If your string is a file path, as in your example, you can also use unix style file paths
string foo = "D:/Projects/Some/Kind/Of/Pathproblem/wuhoo.xml";
But the other answers have the more general solutions to string escaping in C#
You failed to use Google.
Search for "C# escape backslash", and the answer comes up quickly, validating what other posters have said: either use "\\", or use @ to take the string literally.
hey if i escape "\" with "\" it stores the value as "\" itself in my string... pl help me out...
eg:
string cns=".\SQLEXPRESS";
i gave as
string cns=".\SQLEXPRESS" OR @".\SQLEXPRESS"
both stores my cns value as ".\SQLEXPRESS"
pl help me out...