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

.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:

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

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.