Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

file-io

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?

like image 974
Babalola Rotimi Avatar asked Oct 28 '25 09:10

Babalola Rotimi


1 Answers

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.

like image 138
Tim Avatar answered Oct 31 '25 10:10

Tim