Read the appSettings part from an external configuration file

I would like to read the appSettings part of my console application from an external configuration file with the name, say, secrets.config , and I would like to read the rest from it from app.config .

I am currently set up, but it seems that it is not reading from secrets.config , and it does not even tell me about reading failure.

In my app.config

 <appSettings file = "secrets.config"> <add key = "Foo" value = "Bar" /> </appSettings> 

In secrets.config, which is in the same folder as app.config

 <appSettings> <add key = "Secret" value = "Tiger" /> </appSettings> 

In my code

 var secret = ConfigurationManager.AppSettings["Secret"]; // secret turns out to be null 
+5
source share
1 answer

It turns out that I wrote the path to the external file as the wrong path.

From the documentation on this page :

The specified path refers to the main configuration file. For a Windows Forms application, this will be a binary folder (for example, / bin / debug), not the location of the application configuration file. For Web Forms applications, the path refers to the root of the application where the web.config file is located.

I changed the path to the following when working:

 <appSettings file = "..\..\secrets.config"> </appSettings> 
+2
source

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


All Articles