문제

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