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:-

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


About... / Contact.. / Cookie...