2016-11-22 39 views
2

我正在開發使用.NET框架4.6.1一個C#WPF MVVM應用程序,我在App.config中自定義欄目:修改定製的app.config配置部分,並將其保存

<configuration> 
    <configSections> 
     <section name="SpeedSection" type="System.Configuration.NameValueSectionHandler" /> 
    </configSections> 
    <SpeedSection> 
     <add key="PrinterSpeed" value="150" /> 
     <add key="CameraSpeed" value="150" /> 
    </SpeedSection> 
</configuration> 

我想從我的應用修改PrinterSpeedCameraSpeed。我試過這段代碼:

static void AddUpdateAppSettings(string key, string value) 
{ 
    try 
    { 
     var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     var settings = configFile.AppSettings.Settings; 
     if (settings[key] == null) 
     { 
      settings.Add(key, value); 
     } 
     else 
     { 
      settings[key].Value = value; 
     } 
     configFile.Save(ConfigurationSaveMode.Modified); 
     ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); 
    } 
    catch (ConfigurationErrorsException) 
    { 
     Console.WriteLine("Error writing app settings"); 
    } 
} 

但它不起作用,因爲我沒有修改AppSettings部分。

如何修改這些值?

+0

我加入這個作爲一個評論,因爲它不是一個真正的回答你的問題,但可能會幫助你。您是否研究過非常尷尬且令人沮喪的Microsoft解決方案的替代方案。有更好的應用程序配置選擇,例如NuGet Urchin軟件包。 – bikeman868

回答

3

System.Configuration.NameValueSectionHandler很難使用。你可以用System.Configuration.AppSettingsSection替換它不觸及任何東西:

<configuration> 
    <configSections> 
     <section name="SpeedSection" type="System.Configuration.AppSettingsSection" /> 
    </configSections> 
    <SpeedSection> 
     <add key="PrinterSpeed" value="150" /> 
     <add key="CameraSpeed" value="150" /> 
    </SpeedSection> 
</configuration> 

,然後改變你的方法如下:

static void AddUpdateAppSettings(string key, string value) 
{ 
    try 
    { 
     var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     var settings = ((AppSettingsSection) configFile.GetSection("SpeedSection")).Settings;         
     if (settings[key] == null) 
     { 
      settings.Add(key, value); 
     } 
     else 
     { 
      settings[key].Value = value; 
     } 
     configFile.Save(ConfigurationSaveMode.Modified); 
     ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); 
    } 
    catch (ConfigurationErrorsException) 
    { 
     Console.WriteLine("Error writing app settings"); 
    } 
} 
+0

我正在做'((AppSettingsSection)configFile.GetSection(「SpeedSection」))。Settings;'我得到錯誤:'無法將'System.Configuration.KeyValueInternalCollection'轉換爲'System.Configuration.AppSettingsSection'。' – VansFannel

+0

我完全按照答案中的描述測試了此代碼,並確保它可以正常工作。也許你改變了\添加了一些東西? – Evk

+0

是的,對不起。這是我的錯誤。 – VansFannel

0

您應該使用ConfigurationSection類。本教程可能有所幫助:https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

+0

我不確定是否瞭解該教程,但我知道如何訪問該配置數據,但我不知道如何修改它並在App.config上保存更改。 – VansFannel

+0

在你的代碼中你正在訪問appSettings部分並編輯它var settings = configFile.AppSettings.Settings; – Sudet

+0

而不是做它你應該創建你的自定義配置部分類和使用 SpeedSection設置= ConfigurationManager.GetSection(「SpeedSection」)SpeedSection 然後,你可以保存它,因爲你現在正在做它。 – Sudet

相關問題