Reputation: 15289
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?
Upvotes: 3
Views: 454
Reputation: 64487
If the short-hands are valid environment variables, you can resolve their value:
static void Main(string[] args)
{
string val = Environment.ExpandEnvironmentVariables("%USERPROFILE%");
Console.WriteLine(val);
Console.Read();
}
As of .NET 4, special folder support includes user profile:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
Upvotes: 3
Reputation: 175766
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)
Upvotes: 5