2012-12-11 20 views
4

從使用System.Configuration.NameValueSectionHandler定義的部分的配置文件獲取值很容易,當您使用當前配置文件應用程序。當使用ConfigurationManager.OpenMappedExeConfiguration時,如何從ConfigSection中獲取值?使用Configuration.OpenMappedExeConfiguration

示例配置文件。

<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; 

失敗,編譯時出現以下錯誤。

無法通過引用轉換,裝箱轉換,取消裝箱轉換,包裝轉換或空類型轉換將類型「System.Configuration.ConfigurationSection」轉換爲「System.Collections.Specialized.NameValueCollection」。

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)返回System.Configuration.Configuration,Configuration.GetSection返回ConfigurationSection。

ConfigurationManager.GetSection返回對象。

那麼,當我必須使用OpenExeConfiguration時,如何取回我的NameValueCollection?

回答

8

兩年前我遇到了自己的答案。

NameValueSectionHandler - can i use this section type for writing back to the application config file?

這是我的方法來解決我目前的問題。

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; 
} 
+0

不錯的巫術在這一個......! – granadaCoder

相關問題