101

I want to write something like this C:\Users\UserName\Documents\Tasks in a textbox:

txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"\Tasks";

I get the error:

Unrecognized escape sequence.

How do I write a backslash in a string?

9
  • 16
    Use double backslash \\ or put @ at the start of your string Commented Aug 30, 2013 at 12:18
  • stackoverflow.com/questions/1302864/… Commented Aug 30, 2013 at 12:18
  • 2
    @Precious1tj: I would guess maybe they downvoted you because if you googled "C# Unrecognized escape sequence" you would have easily found an answer without having to post a question. Commented Aug 30, 2013 at 12:27
  • @Precious1tj Perhaps because googling your question title would have lead you to an answer? I didn't downvote, so I don't know for certain. Commented Aug 30, 2013 at 12:27
  • 3
    @Precious1tj: I didn't say your title, but your error message. But FYI, for future googlings, be sure to include "C#" in your search. For example, the first result when googling your title with "C#" yields this Commented Aug 30, 2013 at 12:43

6 Answers 6

157

The backslash ("\") character is a special escape character used to indicate other special characters such as new lines (\n), tabs (\t), or quotation marks (\").

If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string:

var s = "\\Tasks";
// or 
var s = @"\Tasks";

Read the MSDN documentation/C# Specification which discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.

Generally speaking, most C# .NET developers tend to favour using the @ verbatim strings when building file/folder paths since it saves them from having to write double backslashes all the time and they can directly copy/paste the path, so I would suggest that you get in the habit of doing the same.


That all said, in this case, I would actually recommend you use the Path.Combine utility method as in @lordkain's answer as then you don't need to worry about whether backslashes are already included in the paths and accidentally doubling-up the slashes or omitting them altogether when combining parts of paths.

Sign up to request clarification or add additional context in comments.

2 Comments

@MattyAB: How are you inspecting the resultant string? If you're checking it out in the Visual Studio debugger, it will show it with escape characters added.
Additionally, using Path.Combine is OS agnostic, so this code could be run on both *nix and windows machines
20

To escape the backslash, simply use 2 of them, like this: \\

If you need to escape other things, this may be helpful..

Comments

6

There is a special function made for this Path.Combine()

var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var fullpath = path.Combine(folder,"Tasks");

Comments

5

Just escape the "\" by using + "\\Tasks" or use a verbatim string like @"\Tasks"

Comments

2
txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"\\\Tasks";

Put a double backslash instead of a single backslash...

Comments

0

even though this post is quite old I tried something that worked for my case .

I wanted to create a string variable with the value below:

21541_12_1_13\":null

so my approach was like that:

  • build the string using verbatim

    string substring = @"21541_12_1_13\"":null";

  • and then remove the unwanted backslashes using Remove function

    string newsubstring = substring.Remove(13, 1);

Hope that helps. Cheers

1 Comment

This would be very cumbersome and error-prone if the string contained multiple backslashes. Also, why insert a backslash just to immediately remove it? newsubstring doesn't contain the desired text, anyways, but substring already does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.