.net – Using environment variables for .config file in .NET


I need to specify path to dlls referenced by assembly in .config file.
Problem is that path can be found in env. variable.
Is it possible to use some sort of %DLLPATH% macro in .config file?

Best Solution

Yes, that's possible! Suppose you have something like that in your config:

    <add key="mypath" value="%DLLPATH%\foo\bar"/>

Then you can easily get the path with:

var pathFromConfig = ConfigurationManager.AppSettings["mypath"];
var expandedPath = Environment.ExpandEnvironmentVariables(pathFromConfig);

ExpandEnvironmentVariables(string s) does the magic by replacing all environment variables within a string with their current values.