Search code examples
.netwindowsspecial-folders

.Net use of special folder notation


I want to be able to do something like this:

IO.Directory.Exists("%USERPROFILE%")

The reason being that I want to specify one of the directories which my application will use, as plain text in a config file. In some cases I will want it to be nested under the user profile, in which case the config file would read something like:

...
LocalDbDirectory = %USERPROFILE%\Application Data\My Toolkit\
...

Or I might want it to be in a network location, in which case it would read something like:

...
LocalDbDirectory = N:\Common\My Toolkit Databases\
...

So I need to be able to interpret the shorthand notation with methods such as IO.Directory.Exists(...) or equivalent.

Any ideas?


Solution

  • You need to run them through Environment.ExpandEnvironmentVariables(path); where path is @"%USERPROFILE%\Application Data\My Toolkit\" (There is no harm in doing this for paths that do not contain %% formatted tokens)