【问题标题】:Reading custom configuration section's key values in C#在 C# 中读取自定义配置部分的键值
【发布时间】:2013-05-13 14:30:47
【问题描述】:

我需要从 app/web.config 中的自定义部分读取键值。

我经历过

Reading a key from the Web.Config using ConfigurationManager

How can I retrieve list of custom configuration sections in the .config file using C#?

但是,当我们需要明确指定配置文件的路径(在我的例子中,配置文件不在其默认位置)时,它们没有指定如何读取自定义部分

我的 web.config 文件示例:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <MyCustomTag> 
    <add key="key1" value="value1" />
    <add key="key2" value="value2" />
  </MyCustomTag>
<system.web>
  <compilation related data />
 </system.web> 
</configuration>

其中我需要读取 MyCustomTag 中的键值对。

当我尝试时(configFilePath 是我的配置文件的路径):-

var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };

var config =
          ConfigurationManager.OpenMappedExeConfiguration(
            configFileMap, ConfigurationUserLevel.None);

        ConfigurationSection section = config.GetSection(sectionName);

        return section[keyName].Value;

我在 [keyName] 部分收到一条错误消息,指出“无法在此处访问受保护的内部索引器 'this'”

【问题讨论】:

  • 请添加您的 web.config(完整或部分)。
  • 与web.config中自定义设置的路径无关。你将使用&lt;MyCustomTag configSource="customConfig.config" /&gt;
  • @FrancescoDeLisi 问题已编辑。
  • @saravanan 在这种情况下,customConfig 是什么?
  • @user85030:该文件将包含您在&lt;add ... 中的所有&lt;add ... 元素&lt;MyCustomTag&gt;

标签: c# configuration-files


【解决方案1】:

不幸的是,这并不像听起来那么容易。解决问题的方法是使用ConfigurationManager 获取文件配置文件,然后使用原始xml。所以,我通常使用以下方法:

private NameValueCollection GetNameValueCollectionSection(string section, string filePath)
{
        string file = filePath;
        System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
        NameValueCollection nameValueColl = new NameValueCollection();

        System.Configuration.ExeConfigurationFileMap map = new ExeConfigurationFileMap();
        map.ExeConfigFilename = file;
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        string xml = config.GetSection(section).SectionInformation.GetRawXml();
        xDoc.LoadXml(xml);

        System.Xml.XmlNode xList = xDoc.ChildNodes[0];
        foreach (System.Xml.XmlNode xNodo in xList)
        {
            nameValueColl.Add(xNodo.Attributes[0].Value, xNodo.Attributes[1].Value);

        }

        return nameValueColl;
 }

以及方法的调用:

 var bla = GetNameValueCollectionSection("MyCustomTag", @".\XMLFile1.xml");


for (int i = 0; i < bla.Count; i++)
{
    Console.WriteLine(bla[i] + " = " + bla.Keys[i]);
}

结果:

【讨论】:

  • 这个答案没有提到为什么这种方法比定义自定义部分类更好(它对于简单的情况来说更直接,但仍然)。
  • @Kai 谢谢。我已经接受了你的回答。 @".\XMLFile1.xml" 是配置文件的路径吧?
  • @Kai 有效!此外,我已将 NameValueCollection 替换为字典以获得更好的性能。
【解决方案2】:

Formo 让它变得非常简单,例如:

dynamic config = new Configuration("customSection");
var appBuildDate = config.ApplicationBuildDate<DateTime>();

Formo on Configuration Sections

【讨论】:

    猜你喜欢
    • 2012-03-05
    • 2012-01-28
    • 2011-12-10
    • 2012-12-12
    • 2018-02-13
    • 2011-07-04
    • 2012-10-04
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多