How to get the Location of the "My Documents" folder using C#
Windows has a special folder designated where people can put their files. It is referred to as "My Documents". It's not a mandatory location, and you can choose your own Location if you wish. The directory is usually the default location, albeit a sub-folder for most applications. As the folder can change between versions of the O.S., there is a .NET function you can call to identify the folder's location. The function is -
Environment.GetFolderPath(Environment.SpecialFolder)
The return string does not contain an ending slash, so you must append it to the end of the function result. The function takes one parameter, which is an enumeration. The principle values of the enumeration, that is, the folders that you are most likely to need, are:-
- ApplicationData - Location of where applications can store in subfolders information about records, e.g. C:\Users\[User1]\AppData\Roaming
- CommonDocuments - Location for the storage of files so other people can access them, e.g. C:\Users\Public\Documents
- CommonProgramFiles - Place where applications store common files, e.g. C:\Program Files (x86)\Common Files
- MyDocuments - Location of the MyDocuments, the target of the question, e.g. C:\users\[User1]\Documents
- MyPictures - Location of where pictures are usually stored, e.g. C:\users\[User1]\pictures
- ProgramFiles - The location of the program files directory shows "C:\Program Files (x86)" instead of "C:\Program Files"
- Windows - Location of where Windows is installed, e.g. C:\Windows
You shouldn't put all your files in that directory. Instead, you should create a subfolder and use that directory to make things tidier. A lot of commercial applications have dedicated subfolders in the My Documents folder. When using a subfolder, be sure to check for the existence of the folder and if it doesn't, create the folder.
Tags - .NET
Last Modified : June 2023