Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# won't escape "\"? [duplicate]

Tags:

c#

Possible Duplicate:
How to use “\” in a string without making it an escape sequence - C#?

Why is it giving me an error in C# when I use a string like: "\themes\default\layout.png"? At the "d" and "l" location? It says unrecognized escape sequence. And how do I stop it from giving me an error when I use: "\"?

Thans

like image 367
Eric Avatar asked Feb 17 '26 11:02

Eric


2 Answers

You need to escape it with an additional \:

string value = "\\themes\\default\\layout.png";

or use the @ symbol:

string value = @"\themes\default\layout.png";

which will avoid you from doubling all \.

Or if you are dealing with paths (which is what it seems you are) you could use the Path.Combine method:

string value = Path.Combine(@"\", "themes", "default", "layout.jpg");
like image 112
Darin Dimitrov Avatar answered Feb 19 '26 00:02

Darin Dimitrov


You're using a backslash to escape 't' and 'd'. If you want to escape the actual backslash you need to do so:

"\\themes\\default\\layout.png"
like image 45
Nick Rolando Avatar answered Feb 19 '26 01:02

Nick Rolando



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!