Read the separate web.config file outside the application folder

I need to read the web.config , outside the application folder (located in any other directory). I tried this code:

 string filePath = @"C:\Users\Idrees\Downloads\New folder\Web.config"; Configuration c1 = ConfigurationManager.OpenExeConfiguration(filePath); var value1 = c1.AppSettings.Settings["Key1"].Value; 

But this gives me an error:

The reference to the object is not installed in the instance of the object.

Because here c1.AppSettings is an object, but c1.AppSettings.Settings does not contain elements (hence, 0 Count). It does not load AppSettings keys. When you try to read any Key collection from Settings it gives this error.

Is there any way to download AppSettings keys from the web.config outside the application folder.

If I put the same file in the application folder, it will successfully read the keys.

This is my example configuration file contents:

 <?xml version="1.0" encoding="utf-8"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <connectionStrings> <!--here goes my connection strings--> </connectionStrings> <appSettings> <add key="Key1" value="Value1" /> <add key="Key2" value="Value2" /> <add key="Key3" value="Value3" /> </appSettings> </configuration> 

I have a web application already running on my server. And I need to develop a small utility that should do some work in the database, and I don’t want to write db credentials or the connection string (and some other advanced application settings) in each application, I want it to read the same from the web .config.

+5
source share
3 answers

You can use the ConfigurationManager to read arbitrary configuration files by opening the exe configuration with the display as follows:

 var filePath = @"C:\Users\Idrees\Downloads\New folder\Web.config"; var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = filePath }; var configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); var value = configuration.AppSettings.Settings["Key1"].Value; 
+3
source

Read the documentation: ConfigurationManager.OpenExeConfiguration on MSDN

 public static Configuration OpenExeConfiguration( string exePath ) 

This is the way to exe

+1
source

As I understand from your comment, you want to have some kind of common configuration across multiple applications on the same computer. You can use an external file as follows:

 <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <connectionStrings configSource="config\connString01.config"/> <appSettings file="config\config01.config"> <add key="Var3" value="Var3 value from main config file"/> </appSettings> 

in the above example .config connectionStrings is obtained from another file. Below is an example of what might be such an external configuration file:

 <connectionStrings> <add name="SQLConnectionString01" connectionString="Data Source=sourcename01;Initial Catalog=cat01;Persist Security Info=True;Integrated Security=true;"/> </connectionStrings> 
+1
source

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


All Articles