2010-03-05 32 views
9

有與reading settings from a separate config file和其他類似的問題,但我的問題是特定於應用程序屬性設置(即<MyApplication.Properties.Settings> - 請參閱下面的XML文件)以及如何動態加載它們。我試着在this post的方法,其中涉及刷新主配置文件的整個appSettings部分,但我適應拋出異常,因爲我並沒有更換appSettings部分:如何動態加載單獨的應用程序設置文件並與當前設置合併?

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal); 
// Have tried the other ConfigurationUserLevels to no avail 
config.AppSettings.File = myRuntimeConfigFilePath; 
config.Save(ConfigurationSaveMode.Modified); // throws ConfigurationErrorsException 
ConfigurationManager.RefreshSection("userSettings"); 

的ConfigurationErrorsException.Message是「根元素必須與引用文件'appSettings'(C:\ myFile.xml第2行)「部分的名稱相匹配。」該文件是:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <userSettings> 
     <MyApplication.Properties.Settings> 
      <setting name="SineWaveFrequency" serializeAs="String"> 
       <value>6</value> 
      </setting> 
      <setting name="SineWaveAmplitude" serializeAs="String"> 
       <value>6</value> 
      </setting> 
     </MyApplication.Properties.Settings> 
    </userSettings> 
</configuration> 

有沒有辦法來導入此文件中的值到MyApplication.Properties.Settings.Default類,與框架處理所有的XML序列化時一樣的配置文件被加載應用程序啓動它呢?

+0

如果您對動機感興趣,請參閱我之前的問題:[如何使用.NET配置文件(app.config,settings.settings)來保存和恢復所有應用程序數據?](http:// stackoverflow。 COM /問題/ 1869628 /如何使用的淨配置 - 文件 - 應用程序 - 配置 - 設置 - 設置對保存和-R)。那時候,我不知道app.config和user.config之間的區別,但我一定在尋找一個解決方案來修改user.config。 – Pat 2010-03-05 21:46:42

回答

7

那麼,這個工程:

using System; 
using System.Configuration; 
using System.IO; 
using System.Linq; 
using System.Xml.Linq; 
using System.Xml.XPath; 

public static class SettingsIO 
{ 
    internal static void Import(string settingsFilePath) 
    { 
     if (!File.Exists(settingsFilePath)) 
     { 
      throw new FileNotFoundException(); 
     } 

     var appSettings = Properties.Settings.Default; 
     try 
     { 
      var config = 
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal); 

      string appSettingsXmlName = 
Properties.Settings.Default.Context["GroupName"].ToString(); 
// returns "MyApplication.Properties.Settings"; 

      // Open settings file as XML 
      var import = XDocument.Load(settingsFilePath); 
      // Get the whole XML inside the settings node 
      var settings = import.XPathSelectElements("//" + appSettingsXmlName); 

      config.GetSectionGroup("userSettings") 
       .Sections[appSettingsXmlName] 
       .SectionInformation 
       .SetRawXml(settings.Single().ToString()); 
      config.Save(ConfigurationSaveMode.Modified); 
      ConfigurationManager.RefreshSection("userSettings"); 

      appSettings.Reload(); 
     } 
     catch (Exception) // Should make this more specific 
     { 
      // Could not import settings. 
      appSettings.Reload(); // from last set saved, not defaults 
     } 
    } 

    internal static void Export(string settingsFilePath) 
    { 
     Properties.Settings.Default.Save(); 
     var config = 
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal); 
     config.SaveAs(settingsFilePath); 
    } 
} 

的導出方法創建像文件中:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <userSettings> 
     <MyApplication.Properties.Settings> 
      <setting name="SineWaveFrequency" serializeAs="String"> 
       <value>1</value> 
      </setting> 
      <setting name="SineWaveAmplitude" serializeAs="String"> 
       <value>100</value> 
      </setting> 
      <setting name="AdcShift" serializeAs="String"> 
       <value>8</value> 
      </setting> 
      <setting name="ControlBitsCheckedIndices" serializeAs="String"> 
       <value>0,1,2,3,5,6,7,8</value> 
      </setting> 
      <setting name="UpgradeSettings" serializeAs="String"> 
       <value>False</value> 
      </setting> 
     </MyApplication.Properties.Settings> 
    </userSettings> 
</configuration> 

導入方法解析該文件,採取節點內的一切,把結果XML到相應部分的user.config文件中,然後重新加載Properties.Settings.Default以從新的user.config文件中獲取這些值。

+0

請注意,您必須添加對System.Configuration程序集的引用。 – Pat 2010-03-10 22:10:26

0

由專利提出的解決方案:

// Get the whole XML inside the settings node 
var settings = import.XPathSelectElements("//" + appSettingsXmlName); 

返回null。我把它改成了

var settings = import.Element("configuration").Element("userSettings").Element(appSettingsXmlName); 

config.GetSectionGroup("userSettings") 
     .Sections[appSettingsXmlName] 
     .SectionInformation 
     .SetRawXml(settings.ToString()); 

它完美地工作。

相關問題