Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

app config - How to read values from multiple Configuration file in c# within a single project?

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

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

Tried the below code but no use:

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
config.AppSettings.File = "App2.config";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See here.

Put this in your App.config:

<appSettings file="accessLevel.config"/>

And then have another file called accessLevel.config like this:

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

And then you can access your config values in code like this:

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...