如何在单个项目中读取在c#多的配置文件值? [英] How to read values from multiple Configuration file in c# within a single project?

查看:268
本文介绍了如何在单个项目中读取在c#多的配置文件值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面在我的项目我有两个叫做应用程序配置文件的app.config accessLevel.config 。现在,使用 OpenExeConfiguration 我能够访问 app.config.exe文件,而不是 accessLevel.config 。请对此有所帮助。

主要的原因我有2个配置文件就是以示区别,使code简单。
我需要在我的 C# code从 accessLevel.config 读出值。

试过低于code,但没有用:

  System.Configuration.Configuration配置= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.File =App2.config;


解决方案

请参阅的这里

将这个你的的App.config

<文件的appSettings =accessLevel.config/>

再有这样叫accessLevel.config另一个文件:

<?XML版本=1.0编码=UTF-8&GT?;
<&的appSettings GT;
  <添加键=TestSettingVALUE =TestValue/>
< /的appSettings>

然后你就可以访问code你的配置价值是这样的:

字符串值= ConfigurationManager.AppSettings [TestSetting];

确保accessLevel.config设置复制到输出目录(右键单击在Visual Studio中的文件 - >属性 - >复制到输出目录 - >复制如果更新版本)。

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 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).

这篇关于如何在单个项目中读取在c#多的配置文件值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆