5

I want to open a xxx.txt file kept on desktop of my Computer but the program gives an

error Parser error unrecognized escape sequence '\D'. I am trying to give the path of the

file as "C:\Documents and Settings\user\Desktop\xxx.txt" .

Am i giving the path in a right way or is there any other way to give it

Sarao
  • 379
  • 1
  • 7
  • 18

5 Answers5

30

\ is an escape character in C# strings. It is used for special characters, such as line break (\n). To write a literal \ you have to quote with another \:

string myFileName = "C:\\Documents and Settings\\user\\Desktop\\xxx.txt";

An alternative is to disable quoting for the string with the @ character:

string myFileName = @"C:\Documents and Settings\user\Desktop\xxx.txt";
Anders Abel
  • 67,989
  • 17
  • 150
  • 217
12

Use this path:

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "xxx.txt");
ionden
  • 12,536
  • 1
  • 45
  • 37
2

I had to access a file in my project, so the folder 'lib' which contains all the files i need, i placed this folder in the 'bin' folder of my project, and now i can access any file i need from lib folder. In code path i used is as follows:

StreamReader sr = new StreamReader("..\\lib\\myFile.src");

Works well! :)

Sabeen
  • 53
  • 5
1

Change your path to C:\\Documents and Settings\\user\\Desktop\\xxx.txt.

Xenon
  • 3,174
  • 18
  • 37
llj098
  • 1,404
  • 11
  • 13
-7

Try to use C:\Documents and Settings\user\Desktop/xxx.txt

Instead of C:\Documents and Settings\user\Desktop\xxx.txt

las
  • 196
  • 8