3

Hi I need to store file path of my folder on as string variable in ASP.Net MVC 4 but when I'm using the following method it shows an error

Unrecognized Escape sequence

static string path="C:\Path";

What is the reason of this error and how can I solve this????

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928

2 Answers2

7

You need to escape the '\' with another '\', like so:

static string path="C:\\Path";

or put an '@' in front of it like so:

static string path = @"C:\Path";

Duplicate: Unrecognized escape sequence for path string containing backslashes

Related reading: 2.4.4.5 String literals

Community
  • 1
  • 1
Mr. Mr.
  • 4,257
  • 3
  • 27
  • 42
2

This is because a backslash in C# is used to mark the next character as its literal interpretation. For example if you wanted a quote inside your string you would precede it with \ to prevent the string being closed early:

var myString = "This is my string with \"quotes\"";

This is called escaping. In order to display a blackslash within a string you either need to escape it with another slash:

static string path = "C:\\Path";

Or precede the string with the @ symbol, which suppresses the backslash escape mechanism:

static string path = @"C:\Path";
Maloric
  • 5,525
  • 3
  • 31
  • 46