单一的app.config多项目C# [英] single app.config multi-project c#

查看:144
本文介绍了单一的app.config多项目C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过3个不同的项目中使用一个单一的app.config。

如何访问配置?

  ConfigurationManager.AppSettings [CONFIG1]


解决方案

常见的配置文件

<?XML版本=1.0编码=UTF-8&GT?;
<结构>
    < configSections>
        <节
            NAME =的appSettings
            键入=System.Configuration.AppSettingsSection,System.Configuration,版本= 2.0.0.0,文化=中性公钥= b03f5f7f11d50a3a
            />
    < / configSections>
    <&的appSettings GT;
        <添加键=KEY1VALUE =值1/>
    < /的appSettings>
< /结构>

要访问映射配置文件

  ConfigurationFileMap fileMap =新ConfigurationFileMap(文件); //路径您的配置文件
配置配置= ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
字符串值= configuration.AppSettings.Settings [键1]值。

I want to use a single app.config by 3 different projects.

How to access the configurations?

ConfigurationManager.AppSettings["config1"]

解决方案

The common config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section 
            name="appSettings" 
            type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
            />
    </configSections>
    <appSettings>
        <add key="key1" value="value1"/>
    </appSettings>
</configuration>

To access mapped config file

ConfigurationFileMap fileMap = new ConfigurationFileMap(file); //Path to your config file
Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
string value = configuration.AppSettings.Settings["key1"].Value;

这篇关于单一的app.config多项目C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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