问题
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
回答1:
\
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";
回答2:
Use this path:
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "xxx.txt");
回答3:
Change your path to C:\\Documents and Settings\\user\\Desktop\\xxx.txt
.
回答4:
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! :)
回答5:
Try to use C:\Documents and Settings\user\Desktop/xxx.txt
Instead of C:\Documents and Settings\user\Desktop\xxx.txt
来源:https://stackoverflow.com/questions/10083651/giving-path-of-a-file-in-c-sharp