【问题标题】:C# - How to Determine if a Configuration Section exists in a mapped EXE configuration file?C# - 如何确定映射的 EXE 配置文件中是否存在配置节?
【发布时间】:2019-10-28 09:28:30
【问题描述】:

我有一个 C# 项目,它正在从名为 test.config 的独立配置文件中读取数据。这是与典型App.config 不同的单独 文件。

我正在尝试确定test.config 文件是否包含来自代码的可选属性TestProperty。我尝试使用TestProperty.ElementInformation.IsPresent,但这总是会导致 FLASE 的值,即使节元素实际上存在。

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?
    }
}

test.config 文件如下所示:

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

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

支持类是:

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; }
    }
}

如果我将该部分移动到 App.config 并使用 ConfigurationManager.GetSection("TestConfigSection"),IsPresent 似乎可以正常工作,但我需要它从单独的文件 (test.config) 中工作。

有没有什么方法可以让TestProperty.ElementInformation 工作或任何其他方法来确定test.config 文件是否包含TestProperty 属性?

【问题讨论】:

  • 我把它放在一个单元测试中,运行它,它对我有用。这让我想知道您是否可能有两个版本的文件。您可以查看ElementInformationSectionInformation 以获取其他线索。这些东西曾经给我带来疯狂的悲伤。如果可以的话,您可能会发现从 JSON 文件或其他文件中读取数据会更容易,而且可以省去麻烦。
  • 有趣,即使将 testconfig 部分放在单独的 xml 配置文件中,它也能正常工作? (即 - 不在 app.config 中,而是在名为 test.config 的单独文件中)
  • 我使用了一个变量,这样我就有了 FileMap 的正确路径。这就是我讨厌这些课程的原因。如果他们不加载我想要一个例外 - 不让它看起来像它加载但值是默认值。我敢肯定他们有他们的理由,但这是一个巨大的痛苦。

标签: c# configuration configuration-files configurationmanager


【解决方案1】:

也许这是你的问题:

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

ExeConfigFilename 不应该是这样的文件的完整路径吗?

fileMap.ExeConfigFilename = filePath;

如果这不是问题,我最近不得不做一些像你正在做的事情,这就是我所做的(使用你的示例数据)。

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;
}

在我的配置文件中,我使用了这种格式:

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

【讨论】:

  • 就是这样!我的问题是我将 fileMap.ExeconfigFileName 设置为文件名,而不是完整路径+文件名。很好的收获,谢谢!
  • ***另外,我需要添加一个重要说明:我不应该调用ExeConfigurationFileMap 的重载,它接受string machineConfigFileName 并同时设置fileMap.ExeConfigFileName!这导致我的代码中出现错误“已添加条目'MyEntry'”我删除了ExeConfigurationFileMap(filePath)并替换为默认构造函数,之后它工作正常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-14
  • 2020-09-07
  • 2016-11-08
  • 2012-03-30
  • 2020-11-13
  • 2018-08-20
相关资源
最近更新 更多