我遇到了一个问题,我无法解析文档标准配置。
例如:
private string GetConfigKey(string param)
{
Configuration dllConfig = ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location);
AppSettingsSection dllConfigAppSettings = (AppSettingsSection)dllConfig.GetSection("appSettings");
return dllConfigAppSettings.Settings[param].Value;
}
因此,我从表单中的文件中获取设置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="host" value="mail.ololo.by"/>
</appSettings>
<configSections>
<section name="provideConfig" type="System.Configuration.DictionarySectionHandler"/>
<section name="provideMailStatus" type="System.Configuration.DictionarySectionHandler" />
</configSections>
<provideConfig>
<add key="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="23534c505763444257460d4c4f4c4f4c0d415a" rel="noreferrer noopener nofollow">[email protected]</a>" value="Mail Subject 1"/>
<add key="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="096e7c6e68496e687d6c276665666566276b70" rel="noreferrer noopener nofollow">[email protected]</a>" value="Mail Subject 2"/>
</provideConfig>
<provideMailStatus>
<add key="status1" value="send"/>
<add key="status2" value="draft"/>
<add key="status2" value="other"/>
</provideMailStatus>
</configuration>
但是
Hashtable hashtable =
(Hashtable)ConfigurationManager.GetSection("provideConfig");
foreach (DictionaryEntry dictionaryEntry in hashtable)
{
Console.WriteLine(dictionaryEntry.Key+" "+dictionaryEntry.Value);
}
但不幸的是 configSections 有问题。我似乎无法理解。 MB,我走错方向了?
附注配置文件不能命名为“app.config”
- 仅项目 dll 名称
最佳答案
配置文件应命名为可执行文件名和“.config”。即使这个可执行文件也使用这个 dll 文件。 例如:ConSoleApplication.exe 使用 MyLibrary.dll。然后配置文件必须命名为ConSoleApplication.exe.config
如果您需要配置文件的其他名称,请阅读 this
关于c# - 我的 dll 配置文件或为什么无法解析部分..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12705294/