I'd like to read a part of the appSettings
of my console application from an external configuration file named, say, secrets.config
, while the rest of it I would like to read from the app.config
.
Presently, I have this set up but it seems like it isn't reading from secrets.config
and it isn't even telling me about the read 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
The <appSettings> element of a web. config file is a place to store connection strings, server names, file paths, and other miscellaneous settings needed by an application to perform work.
This file is located in a Virtual Directory (AppSettings) located within the website or application (if running Aeries as an application) in IIS. The physical directory should be located in some other location on the file system and NOT within the inetpub or website folders.
It turns out that I was writing the path of the external file as the wrong path.
From the documentation on this page:
The path specified is relative to the main configuration file. For a Windows Forms application, this would be the binary folder (such as /bin/debug), not the location of the application configuration file. For Web Forms applications, the path is relative to the application root, where the web.config file is located.
I changed the path to the following at it worked:
<appSettings file = "..\..\secrets.config">
</appSettings>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With