Pregunta

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?

¿Fue útil?

Solución

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"));

Otros consejos

string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
StreamReader fileitem = new StreamReader(Path.Combine(documents, "dataset.txt");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top