Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using environment variables for .config file in .NET

Tags:

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?

like image 755
Jox Avatar asked Dec 11 '08 07:12

Jox


People also ask

How do you put environmental variables in Web config?

For Applications, including Web Applications, On Windows: If you want environmental variables to be expanded your application will need to do that itself. A common way of doing this is to use the cmd syntax %variable% and then using Environment. ExpandEnvironmentVariables to expand them.

Which environment values are Bydefault supported by .NET Core?

Built-In Environments in . When checking the ASP.NET core project template, you should see that the “ASPNETCORE_ENVIRONMENT” variable with the value “Development” is set by default. Those are three values that are used by convention: Development, Staging, and Production.


1 Answers

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.

like image 54
Prensen Avatar answered Oct 20 '22 06:10

Prensen