How to read values ​​from several configuration files in C # within the same project?

In my project, I have two application configuration files called app.config and accessLevel.config . Now, using OpenExeConfiguration , I was able to access the app.config.exe file , but not accessLevel.config . Please help with this.

The main reason I have 2 configuration files is to show the difference and make the code simple. I need to read the values ​​from accessLevel.config in my C# code.

Tried the code below but didn't use:

 System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.File = "App2.config"; 
+7
source share
1 answer

See here .

Put this in your App.config :

 <appSettings file="accessLevel.config"/> 

And another file called accessLevel.config:

 <?xml version="1.0" encoding="utf-8" ?> <appSettings> <add key="TestSetting" value="TestValue"/> </appSettings> 

And then you can access your configuration values ​​in code as follows:

 string value = ConfigurationManager.AppSettings["TestSetting"]; 

Make sure accessLevel.config is set to copy to the output directory (right-click the file in Visual Studio -> Properties -> Copy to output directory -> Copy if new).

+19
source

All Articles