Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read separate web.config file outside the application folder

Tags:

c#

asp.net

config

I need to read web.config file, outside the application's 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 it is giving me the error:

Object reference not set to an instance of an object.

Because here, c1.AppSettings is an object, but c1.AppSettings.Settings contains not items (hence 0 Count). It is not really loading the AppSettings keys. When trying to read any Key from Settings collection, it gives this error.

Is there any way how to load AppSettings keys from a web.config file outside the application folder.

If I put same file within application folder, then it reads the keys successfully.

This is my sample config file's content:

<?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 which has to do some job in database, and I don't want to write db credentials or connection string(and some other additional app-settings) in each application, I want it to read same thing from web.config.

like image 359
M_Idrees Avatar asked Mar 20 '18 07:03

M_Idrees


People also ask

How do I use different Web config files?

config (or any file) when you press F5 in Visual Studio. You can have different transformations based on the build configuration. This will enable you to easily have different app settings, connection strings, etc for Debug versus Release. If you want to transform other files you can do that too.

Can we have more than one Web config file in a application?

Yes you can have two web. config files in application. There are situations where your application is divided in to modules and for every module you need separate configuration. For example if you have a application which has two modules lets say accounts and sales.


1 Answers

You can using ConfigurationManager to read arbitrary configuration files by opening a mapped exe configuration 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;
like image 186
yaakov Avatar answered Oct 17 '22 21:10

yaakov