首页 > 解决方案 > C# - How to Determine if a Configuration Section exists in a mapped EXE configuration file?

问题描述

I have a C# project that is reading from a stand-alone configuration file named test.config. This is a separate file from the typical App.config.

I am trying to determine if the test.config file contains the optional property TestProperty from code. I attempted to use TestProperty.ElementInformation.IsPresent but this always results in a value of FLASE even when the section element is actually there.

class Program
{
    static void Main(string[] args)
    {
        string filePath = @"C:\Users\username\Desktop\TestProject\ConfigTestApp\Test.Config";
        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(filePath);
        fileMap.ExeConfigFilename = Path.GetFileName(filePath);

        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        TestConfigSection section = config.GetSection("TestConfigSection") as TestConfigSection;
        bool isPresent = section.TestProperty.ElementInformation.IsPresent; // Why is this always false?
    }
}

The test.config file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name ="TestConfigSection" type ="ConfigTestApp.TestConfigSection, ConfigTestApp"/>
  </configSections>

  <TestConfigSection>
    <TestProperty testvalue="testing 123" />
  </TestConfigSection>
</configuration>

The backing classes are:

public class TestConfigSection : ConfigurationSection
{
    [ConfigurationProperty("TestProperty", IsRequired = true)]
    public TestConfigElement TestProperty
    {
        get
        {
            return base["TestProperty"] as TestConfigElement;
        }
    }
}

public class TestConfigElement : ConfigurationElement
{
    [ConfigurationProperty("testvalue", IsKey = true, IsRequired = true)]
    public string TestValue
    {
        get { return base["testvalue"] as string; }
        set { base["testvalue"] = value; }
    }
}

If I move the section into App.config and use ConfigurationManager.GetSection("TestConfigSection"), IsPresent seems to work fine, but I need this to work from a separate file (test.config).

Is there any way to get TestProperty.ElementInformation working or any other way to determine if the test.config file contains the TestProperty property?

标签: c#configurationconfiguration-filesconfigurationmanager

解决方案


Perhaps this is your problem:

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(filePath);
fileMap.ExeConfigFilename = Path.GetFileName(filePath);

Shouldn't ExeConfigFilename be the full path to the file like this?

fileMap.ExeConfigFilename = filePath;

If that is not the problem, I recently had to do something like you are doing and here is what I did (using your example data).

string filePath = @"C:\Users\username\Desktop\TestProject\ConfigTestApp\Test.Config";
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
AppSettingsSection section = (AppSettingsSection) config.GetSection("TestConfigSection");

if ( section != null )
{
  string testValue = section .Settings["TestProperty"].Value;
}

In my config file I used this type of format:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <TestConfigSection file="">
    <clear />
    <add key="TestProperty" value="testing 123" />
  </TestConfigSection>
</configuration>

推荐阅读