2016-11-22 41 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

相关问题