Change configuration settings

I have a configuration file that is used in several projects, general.config , looks like this:

 <?xml version="1.0" encoding="utf-8" ?> <appSettings> <add key="mykey1" value="myvalue1"/> <add key="mykey2" value="myvalue2"/> </appSettings> 

In one of the projects, I need to override one of two settings. So the app.config this project is as follows:

 <?xml version="1.0"?> <configuration> <appSettings file="general.config"> <remove key="mykey1"/> <add key="mykey1" value="anothervalue"/> <add key="mykey3" value="myvalue3"/> </appSettings> </configuration> 

But remove doesn't work here. How can I override mykey1 without breaking mykey2 ? add works in this case. I can get myvalue3 from ConfigurationManager .

EDIT: general.config automatically copied to the output folder during compilation. Do not worry about the problem of the path. Currently I have received:

 ConfigurationManager.AppSettings["mykey1"] //I got "myvalue1", but I want "anothervalue" here //that is, this item is "overrided", just like virtual methods in C# ConfigurationManager.AppSettings["mykey2"] //this setting will not be modified, currently it works fine ConfigurationManager.AppSettings["mykey3"] //good 
+8
c # configuration app-config
source share
4 answers

A friend of mine answered this question. From MSDN :

You can use the file attribute to specify a configuration file that provides additional settings or overrides the settings that are specified in the appSettings element. You can use the file attribute in the development of the script versioning command, for example, when the user wants to override the project settings that are specified in the configuration file application. The configuration files specified in the file attribute must have an appSettings element, not the configuration as the root node.

So, in this question, the settings in general.config override the elements in app.config , different from what I think (want) app.config , overrides the elements in general.config . Now I think I need to solve this problem in C # code (this inevitably looks ugly).

+2
source share

Elements are changed from a child element and what I mean by this is your app.config is the parent file, and the values ​​are replaced with the values ​​that exist in General.config

Since you use remove in the parent file, its effective removal is to delete the element that you specified in app.config, but after that the elements from general.config are inserted. Now say here in General.config that you say delete mykey3 , which is on your app.config, you will see that the final collection does not have a key like mykey3 .

In short, this will not work. Hope this helps you.

0
source share

You can add another configuration file, for example Test.config.

 <appSettings> <add key="mykey1" value="New value"/> </appSettings> 

and in the appsettings section, app.config will look like this:

 <appSettings file="Test.config"> <add key="mykey1" value="myvalue1"/> </appSettings> 
0
source share

Using the file attribute to load general parameters with the expectation that keys added directly to the <appSettings> element <appSettings> override these general settings is understandable, but unfortunately this is not how it works.

Microsoft intends to use the file attribute to load general parameters that override individual application settings.

This is discussed in detail in the Microsoft Documentation.

To solve this problem, we very often declare the basic settings in a common file, and then appropriately call overrides in the application configuration. However, this requires additional code, which is a bit ugly. eg.

 var config = ConfigurationManager.AppSettings["MSG_QUEUE_PROVIDER_OVERRIDE"] ?? ConfigurationManager.AppSettings["MSG_QUEUE_PROVIDER"] ?? "ActiveMQ"; 

 <appSettings file="common.config"> <!-- Override the common values --> <add key="MSG_QUEUE_PROVIDER_OVERRIDE" value="RabbitMQ"/> </appSettings> 
0
source share

All Articles