85

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?

TylerH
  • 20,816
  • 57
  • 73
  • 92
  • 14
    Use double backslash \\ or put @ at the start of your string – Andrew Aug 30 '13 at 12:18
  • http://stackoverflow.com/questions/1302864/unrecognized-escape-sequence-for-path-string-containing-backslashes – Charles Barthélemy Aug 30 '13 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. – Chris Sinclair Aug 30 '13 at 12:27
  • @Precious1tj Perhaps because [googling your question title](https://www.google.com/search?q=How+do+i+write+a+backslash+(%5C)+in+a+string) would have lead you to an answer? I didn't downvote, so I don't know for certain. – Nolonar Aug 30 '13 at 12:27
  • @Nolonar: I like how the first result is _this question_. [**INCEPTION**](http://inception.davepedu.com/) – Chris Sinclair Aug 30 '13 at 12:29
  • @ChrisSinclair I know; this just shows how fast Google is. However, the "first" result is not the only one with valid answers. – Nolonar Aug 30 '13 at 12:32
  • @Nolonar: I know, just fun to see. :) – Chris Sinclair Aug 30 '13 at 12:40
  • @ChrisSinclair How was i supposed to know???..I googled my title and didn't find anything helpful –  Aug 30 '13 at 12:41
  • 3
    @Precious1tj: I didn't say your title, but your [error message](https://www.google.ca/webhp#psj=1&q=Unrecognized+escape+sequence). 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](http://forums.asp.net/t/1275075.aspx/1) – Chris Sinclair Aug 30 '13 at 12:43
  • @ChrisSinclair Thanks –  Aug 30 '13 at 12:44

7 Answers7

133

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.

CodeCaster
  • 139,522
  • 20
  • 204
  • 252
Chris Sinclair
  • 22,250
  • 3
  • 50
  • 90
  • 4
    @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. – Chris Sinclair Aug 14 '16 at 13:33
  • Additionally, using `Path.Combine` is OS agnostic, so this code could be run on both *nix and windows machines – Stephen Wigginton Aug 06 '20 at 19:20
19

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

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

Kyle
  • 543
  • 2
  • 7
6

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

var folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
var fullpath = path.Combine(folder,"Tasks");
lordkain
  • 2,993
  • 1
  • 12
  • 17
4

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

StuartLC
  • 100,561
  • 17
  • 199
  • 269
Andre Lombaard
  • 6,745
  • 13
  • 54
  • 96
3

The previous answer is correct but in this specific case I would recommend using the System.IO.Path.Combine method.

You can find more details here: http://msdn.microsoft.com/en-us/library/fyy7a5kt.aspx

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

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

Community
  • 1
  • 1
Nil
  • 151
  • 1
  • 6
1

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

ksereis
  • 11
  • 2