Lir*_*n B 64 c# configurationmanager config configurationsection
我正在使用C#,Framework 3.5(VS 2008).
我正在使用将ConfigurationManager配置(而不是默认的app.config文件)加载到Configuration对象中.
使用Configuration类,我能够得到一个ConfigurationSection,但我找不到获取该部分值的方法.
在配置中,ConfigurationSection是类型System.Configuration.NameValueSectionHandler.
对于什么是价值,当我使用的方法GetSection的ConfigurationManager(只能当它是对我的默认app.config文件),我收到了一个对象类型,我可以投进去对键值的集合,我刚刚收到像字典一样的价值.但是,当我ConfigurationSection从Configuration类接收类时,我无法进行此类转换.
编辑:配置文件的示例:
<configuration>
<configSections>
<section name="MyParams"
type="System.Configuration.NameValueSectionHandler" />
</configSections>
<MyParams>
<add key="FirstParam" value="One"/>
<add key="SecondParam" value="Two"/>
</MyParams>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我在app.config上使用它时的方式示例("GetSection"方法仅适用于默认的app.config):
NameValueCollection myParamsCollection =
(NameValueCollection)ConfigurationManager.GetSection("MyParams");
Console.WriteLine(myParamsCollection["FirstParam"]);
Console.WriteLine(myParamsCollection["SecondParam"]);
Run Code Online (Sandbox Code Playgroud)
ner*_*jus 22
受到确切问题的影响.问题是因为.config文件中的NameValueSectionHandler.您应该使用AppSettingsSection:
<configuration>
<configSections>
<section name="DEV" type="System.Configuration.AppSettingsSection" />
<section name="TEST" type="System.Configuration.AppSettingsSection" />
</configSections>
<TEST>
<add key="key" value="value1" />
</TEST>
<DEV>
<add key="key" value="value2" />
</DEV>
</configuration>
Run Code Online (Sandbox Code Playgroud)
然后在C#代码中:
AppSettingsSection section = (AppSettingsSection)ConfigurationManager.GetSection("TEST");
Run Code Online (Sandbox Code Playgroud)
btw 2.0中不再支持NameValueSectionHandler.
Mon*_*nch 17
这是一篇很好的文章,展示了如何做到这一点.
如果要从app.config以外的文件中读取值,则需要将其加载到ConfigurationManager中.
试试这个方法:ConfigurationManager.OpenMappedExeConfiguration()
有一个如何在MSDN文章中使用它的示例.
Ste*_*all 13
尝试使用AppSettingsSection而不是NameValueCollection.像这样的东西:
var section = (AppSettingsSection)config.GetSection(sectionName);
string results = section.Settings[key].Value;
Run Code Online (Sandbox Code Playgroud)
资料来源:http: //social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d5079420-40cb-4255-9b3b-f9a41a1f7ad2/
我能让它工作的唯一方法是手动实例化段处理程序类型,将原始XML传递给它,并转换生成的对象.
看起来效率很低,但你去了.
我写了一个扩展方法来封装这个:
public static class ConfigurationSectionExtensions
{
public static T GetAs<T>(this ConfigurationSection section)
{
var sectionInformation = section.SectionInformation;
var sectionHandlerType = Type.GetType(sectionInformation.Type);
if (sectionHandlerType == null)
{
throw new InvalidOperationException(string.Format("Unable to find section handler type '{0}'.", sectionInformation.Type));
}
IConfigurationSectionHandler sectionHandler;
try
{
sectionHandler = (IConfigurationSectionHandler)Activator.CreateInstance(sectionHandlerType);
}
catch (InvalidCastException ex)
{
throw new InvalidOperationException(string.Format("Section handler type '{0}' does not implement IConfigurationSectionHandler.", sectionInformation.Type), ex);
}
var rawXml = sectionInformation.GetRawXml();
if (rawXml == null)
{
return default(T);
}
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(rawXml);
return (T)sectionHandler.Create(null, null, xmlDocument.DocumentElement);
}
}
Run Code Online (Sandbox Code Playgroud)
您在示例中调用它的方式是:
var map = new ExeConfigurationFileMap
{
ExeConfigFilename = @"c:\\foo.config"
};
var configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var myParamsSection = configuration.GetSection("MyParams");
var myParamsCollection = myParamsSection.GetAs<NameValueCollection>();
Run Code Online (Sandbox Code Playgroud)