Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is There A System Defined Environment Variable For Documents Directory?

I know about the %USERPROFILE% system defined environment variable on Windows XP (and Vista and Windows 7). Is there a system defined environment variable pointing to the location of the "My Documents" directory? On XP by default it's %USERPROFILE%\My Documents and on Win 7 it's %USERPROFILE%\Documents. I just wanted to avoid having to test for the OS version in a Powershell script if I can avoid it.

like image 343
Onorio Catenacci Avatar asked Aug 16 '10 12:08

Onorio Catenacci


People also ask

How do I get a list of environment variables?

To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.

How do I get to System Variables in environment properties?

In the Power User Task Menu, select the System option. Click the Advanced System Settings link in the left column. In the System Properties window, click the Advanced tab, then click the Environment Variables button near the bottom of that tab.

What is the PATH to Documents in Windows 10?

If you cannot see it by this method, then, in the Run prompt, type %userprofile%\Documents and hit the enter key. It will open the Documents folder.


2 Answers

For powershell the following works:

[environment]::getfolderpath("mydocuments") 

and avoiding magic strings

[Environment]::GetFolderPath([Environment+SpecialFolder]::MyDocuments) 

For .NET the following holds true (ie not applicable in all windows applications):

As one answer points out, there is no Environment Variable pointing to My Documents but there is Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) (C#) for .NET.

I'm adding this answer since this question comes up when googling for C#, environment variables and my documents and Justin's answer does not contain the line of code :)

Using the above mentioned line of code is the preferred way of accessing my documents in .NET :)

Copy paste this row for C# usage:

var directoryNameOfMyDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 

Note that C# needs a capital D in MyDocuments.

like image 89
flindeberg Avatar answered Sep 19 '22 23:09

flindeberg


On my default-installation XP system, there is no environment variable for that. You can list all variables with the "set" command ( no parameters ) in the command line. So probably you have to do a test.

If you don't want to test for the OS version, you can simply check whether "Documents" exists and if not then try "My Documents" or vice versa. This isn't perfect however, because s/o could have a "Documents" folder on his XP machine.

Btw: my system is German, so the folder is called "Dokumente". You might need to take that into account.

The path to that folder is stored in

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders 

under Personal. You need registry access, though.

Source: Microsoft

like image 23
msteiger Avatar answered Sep 18 '22 23:09

msteiger