95

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,799
  • 66
  • 75
  • 101
  • 15
    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

6 Answers6

151

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.

Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93
  • 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
  • 1
    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
20

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

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

Kyle
  • 553
  • 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
  • 3,061
  • 1
  • 13
  • 18
5

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

StuartLC
  • 104,537
  • 17
  • 209
  • 285
Andre Lombaard
  • 6,985
  • 13
  • 55
  • 96
2
txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"\\\Tasks";

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

Community
  • 1
  • 1
Nil
  • 161
  • 1
  • 6
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

ksereis
  • 9
  • 2
  • 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. – Lance U. Matthews Jul 08 '22 at 18:27