c# - 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?
解决方案
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>
推荐阅读
- c# - 如何从不同的任务中增加价值
- javascript - 在父悬停时将传递道具反应给孩子
- python - Django 表单不保存任何信息
- python - 使用 Google Colab 转换我的 Python-Kivy 代码后无法在手机中执行我的 APK 代码
- html - 关闭按钮在我的模态弹出图像中不起作用
- node.js - 从 nodejs 客户端调用 .net core 3.1 gRpc 服务
- php - 如何在 WhatsApp 消息中创建换行符?
- python - discord.py master(v2.0) -- FlagConvert 对象的默认值是什么?
- python - NumPy 屏蔽操作?
- mysql - 在 Arch linux 上运行 mysql_secure_installation 时出错