当你使用应用程序的当前配置文件从使用由System.Configuration.NameValueSectionHandler限定的区间配置文件获取值很容易。
示例配置文件。
<configuration>
<configSections>
<section name="MyParams" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<MyParams>
<add key="FirstParam" value="One"/>
<add key="SecondParam" value="Two"/>
</MyParams>
</configuration>
示例代码,很容易读取它。
NameValueCollection myParamsCollection =
ConfigurationManager.GetSection("MyParams") as NameValueCollection;
这是行不通的代码。
NameValueCollection collection =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
.GetSection("MyParams") as NameValueCollection;
失败与编译下面的错误。
无法通过引用转换,装箱转换,取消装箱转换,包装转换或null类型转换将类型“System.Configuration.ConfigurationSection”到“System.Collections.Specialized.NameValueCollection”。
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)返回一个System.Configuration.Configuration和Configuration.GetSection返回的ConfigurationSection。
ConfigurationManager.GetSection返回对象。
所以,我怎么找回我的NameValueCollection时,我不得不使用OpenExeConfiguration?
我在我自己的答案跑了两年前。
NameValueSectionHandler -我可以使用这款机型的写回应用程序配置文件?
这是我的方法来解决我目前的问题。
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap() {
ExeConfigFilename = "path to config here"
};
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
configFileMap, ConfigurationUserLevel.None);
ConfigurationSection myParamsSection = config.GetSection("MyParams");
string myParamsSectionRawXml = myParamsSection .SectionInformation.GetRawXml();
XmlDocument sectionXmlDoc = new XmlDocument();
sectionXmlDoc.Load(new StringReader(myParamsSectionRawXml ));
NameValueSectionHandler handler = new NameValueSectionHandler();
NameValueCollection handlerCreatedCollection =
handler.Create(null, null, sectionXmlDoc.DocumentElement) as NameValueCollection;
Console.WriteLine(handlerCreatedCollection.Count);
与任何传统IConfigurationSectionHandler类型NameValueSectionHandler,DictionarySectionHandler,SingleTagSectionHandler的工作方法。
public static object GetConfigurationValues(string configFileName, string sectionName)
{
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = configFileName };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
ConfigurationSection section = config.GetSection(sectionName);
string xml = section.SectionInformation.GetRawXml();
XmlDocument doc = new XmlDocument();
doc.Load(XmlReader.Create(new StringReader(xml)));
string type = section.SectionInformation.Type;
string assemblyName = typeof(IConfigurationSectionHandler).Assembly.GetName().FullName;
ObjectHandle configSectionHandlerHandle = Activator.CreateInstance(assemblyName, section.SectionInformation.Type);
if (configSectionHandlerHandle != null)
{
IConfigurationSectionHandler handler = configSectionHandlerHandle.Unwrap() as IConfigurationSectionHandler;
return handler.Create(null, null, doc.DocumentElement);
}
return null;
}
文章来源: How do I get the values from a ConfigSection defined as NameValueSectionHandler when using ConfigurationManager.OpenMappedExeConfiguration