Giving path of a file in C#

浪子不回头ぞ 提交于 2019-12-04 02:18:06

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!