I have the Windows Form Application which works with 2 data sets(text files). How can change the path of text files from C Drive into the Documents folder with following adress: Libraries\Documents? If I want copy them into the desktop what can be the path? PS: I copy the data sets into Documents and change the

StreamReader fileitem = new StreamReader("c:\\dataset.txt");\

into:

StreamReader fileitem = new StreamReader("Libraries\Documents\dataset.txt");

But it doesnot work.

An idea?

有帮助吗?

解决方案

You need Environment.GetFolderPath.

string myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

Don't concatenate strings to create path, use Path.Combine instead. So when you need subfolder of desktop you'll use

string subFolder = Path.Combine(desktop,"MySubFolderName");

So in your case

StreamReader fileitem = new StreamReader(Path.Combine(desktop,"dataset.txt"));

其他提示

string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
StreamReader fileitem = new StreamReader(Path.Combine(documents, "dataset.txt");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top