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

2

5 Answers 5

31

\ 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"; 
Sign up to request clarification or add additional context in comments.

Comments

12

Use this path:

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "xxx.txt"); 

Comments

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! :)

Comments

1

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

Comments

-7

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.