Can we have multiple App.Config files in a .NET console application?

I have a console application in which there is an App.Confile file. Now, environment-specific parameters are saved here.

Now I think that several app.config files (for example, app.dev.config, app.test.config and app.prod.config ) are a way how we can have several Web.Config files.

In the case of a web application, we can handle this and the ConfigurationManager will display the corresponding Web.Config file.

In the case of a console application, I'm not sure. If so, how can we have multiple app.config files?

Appreciate your help.

thanks

+8
c # web-config app-config
source share
2 answers

UPDATE

With Visual Studio 2010 and 2012, you have all been integrated into the IDE. If you right-click your configuration file, VS will give you the ability to generate a conversion configuration for each of your build configurations. If you want to create an assembly configuration for each of your environments, MSBuild will automatically create the correct Web.config / app.config for you.

The short answer is yes. You may have different files in your build script, but you will need to rename the correct option to "App.config" and you will install (before compiling).

The long answer , what you should use is the Enterprise MergeConfiguration tool. This allows you to use the existing App.config as the base and define the delta for the environment. The tool will combine the base and delta to create environment-specificig configuration files. You still need some logic in the build script to apply the correct configuration file.

When you install Enterprise Library on your computer, you can right-click the configuration file in Visual Studio and edit it using the configuration tool. You can use this to define your environments, as well as application settings and connection strings to override for each environment.

http://entlib.codeplex.com/

+8
source share

To execute a response from Babak, you can also separate parts of your configuration from other configuration files using the configSource attribute for any item that represents a ConfigurationSection , for example:

 <appSettings configSource="appSettings.config" /> 

And in appSettings.config:

 <?xml version="1.0" encoding="UTF-8"?> <appSettings> <add key="Blah" value="Nim nim nim" /> </appSettings> 
+8
source share

Source: https://habr.com/ru/post/650212/


All Articles