Babalola Rotimi
Babalola Rotimi

Reputation: 339

How can I get the location of a user's Documents directory?

I want to be able to write a string to a text file on any computer. For example

string line = "Sample text to write"
File.WriteAllText(@"C:\Users\Rolex James\Documents\sample.txt" , line);

On another system this path would be different, is there a way of writing it such that you don't have to modify the path to suit each system you want to run it on?

Upvotes: 4

Views: 4689

Answers (1)

Tim
Tim

Reputation: 15237

It depends on where you want to write it, which isn't completely clear by your question. If you're always looking for the current user's documents, then you should look at the System.Environment.GetFolderPath method and the System.Environment.SpecialFolder.MyDocuments enumeration value in particular. When that executes, it will give you the path to the current user's documents directory, which will vary from user to user and computer to computer.

Upvotes: 8

Related Questions