从定义为NameValueSectionHandler一个ConfigSection使用ConfigurationManager.OpenMappedExeConfiguration时,我如何获得的值 [英] How do I get the values from a ConfigSection defined as NameValueSectionHandler when using ConfigurationManager.OpenMappedExeConfiguration

查看:474
本文介绍了从定义为NameValueSectionHandler一个ConfigSection使用ConfigurationManager.OpenMappedExeConfiguration时,我如何获得的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是使用由System.Configuration.NameValueSectionHandler定义的部分配置文件获取的值,当你使用当前配置文件的应用程序很容易。

Getting the values from a config file that uses a section defined by System.Configuration.NameValueSectionHandler is easy when you're using the current config file for the application.

实例配置文件。

<configuration>
  <configSections>
    <section name="MyParams" type="System.Configuration.NameValueSectionHandler" />
  </configSections>

  <MyParams>
    <add key="FirstParam" value="One"/>
    <add key="SecondParam" value="Two"/>
  </MyParams>
</configuration>

例如code,很容易读取它。

Example Code that easily reads it.

NameValueCollection myParamsCollection =
   ConfigurationManager.GetSection("MyParams") as NameValueCollection;

这是在code不起作用。

This is the code that doesn't work.

NameValueCollection collection =
  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
  .GetSection("MyParams") as NameValueCollection;

这是失败,在编译下面的错误。

That fails with the following error on compile.

无法通过引用转换,装箱转换,取消装箱转换,包装转换或null类型转换将类型System.Configuration.ConfigurationSection'到'System.Collections.Specialized.NameValueCollection。

Cannot convert type 'System.Configuration.ConfigurationSection' to 'System.Collections.Specialized.NameValueCollection' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion.

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)返回一个System.Configuration.Configuration,并Configuration.GetSection返回的ConfigurationSection。

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) returns a System.Configuration.Configuration, and Configuration.GetSection returns ConfigurationSection.

ConfigurationManager.GetSection返回对象。

ConfigurationManager.GetSection returns object.

那么,我怎么找回我的NameValueCollection时,我不得不使用OpenExeConfiguration?

So, how do I get back my NameValueCollection when I have to use OpenExeConfiguration?

推荐答案

我跑过上帮助了很多相同的切线较早的答案。

I ran across an earlier answer on the same tangent which helped alot.

<一个href="http://stackoverflow.com/questions/1149443/namevaluesectionhandler-can-i-use-this-section-type-for-writing-back-to-the-ap">NameValueSectionHandler - 我可以使用这款机型的写回应用程序配置文件

这是我的方法来解决这个问题。

This is my approach to solve this issue.

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。

A method that works with any of the legacy IConfigurationSectionHandler types 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;
}

这篇关于从定义为NameValueSectionHandler一个ConfigSection使用ConfigurationManager.OpenMappedExeConfiguration时,我如何获得的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆