繁体   English   中英

从App.config C#以编程方式获取节名称

[英]Get Section Names Programmatically from App.config C#

有没有办法通过循环而不是自己对名称进行硬编码来以编程方式获取节名称?

例如:

<configuration>
  <configSections>
    <section name="Test1"  type="System.Configuration.NameValueSectionHandler"/>
    <section name="Test2" type="System.Configuration.NameValueSectionHandler"/>
  </configSections>

硬编码:

var section = ConfigurationManager.GetSection("Test1") as NameValueCollection;
var section = ConfigurationManager.GetSection("Test2") as NameValueCollection;

我不想在代码中对Test1和Test2部分的名称进行硬编码。

您可以像下面这样获得节组名称:

    static void Main(string[] args)
    {
        string configPath = @"..\..\App.config";
        var map = new ExeConfigurationFileMap();
        map.ExeConfigFilename = configPath;
        var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        for (int i = 0; i < config.SectionGroups.Count; i++)
        {
            Console.WriteLine(config.SectionGroups[i].SectionGroupName);
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM