11-year update:
After coming from middle-school Windows XP bash scripting to getting a CS degree and ending up in Windows 11 development, I've come to the same conclusion that others have already commented:
THERE IS NO RELIABLE WAY TO GET THIS FOLDER. IF YOU TRY TO YOU WILL EVENTUALLY GET ERRORS.
The folder path will vary from system-to-system and there's no reliable way to verify the folder is the same folder. It's often under %UserProfile%\Documents
but THIS IS NO GUARANTEE AND YOU SHOULD NOT TRUST THIS.
POSSIBLE solutions
So you're desperate? Good luck finding a reliable solution across every version of Windows and every user. That said, I'll provide some aid if you absolutely positively truly have to get this folder for some reason:
- Your code should be testing for different paths to find it
- If it fails to do so, one alternative is to get your user to input it for you
- Alternatively, just use a different folder!
%appdata%
or %localappdata%
are acceptable places, depending on your project
There are ways to get this folder but there's no guarantee on the reliability of any of these.
- PowerShell:
[Environment]::GetFolderPath("MyDocuments")
(enum)
- C#:
Environment.SpecialFolder.MyDocuments
(enum)
- Variety of registry commands:
reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User
Shell Folders" /v personal
reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell
Folders" /v personal