修改自定义app.config配置部分并保存

6
我正在开发一款使用.NET Framework 4.6.1的C# WPF MVVM应用程序,并在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部分。

我该如何修改这些值?


我添加这个评论是因为它并不是回答你的问题,但可能会帮到你。你有没有考虑过微软那个非常笨拙和令人沮丧的解决方案的替代方案?有更好的应用程序配置替代方案,例如 NuGet Urchin 包。 - bikeman868
2个回答

6

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");
    }
}

我正在执行 ((AppSettingsSection) configFile.GetSection("SpeedSection")).Settings;,但是我遇到了错误:无法将 'System.Configuration.KeyValueInternalCollection' 转换为 'System.Configuration.AppSettingsSection'。 - VansFannel
我按照答案中描述的确切测试了这段代码,并确保它可以工作。也许你已经改变或添加了什么东西? - Evk
是的,抱歉。那是我的错误。 - VansFannel

0

我不确定是否理解了那个教程,但我知道如何访问配置数据,但不知道如何修改它并保存更改到 App.config。 - VansFannel
在你的代码中,你正在访问AppSettings部分并编辑它,变量设置=configFile.AppSettings.Settings; - Sudet
你应该创建自定义配置部分类并使用SpeedSection settings = ConfigurationManager.GetSection("SpeedSection") as SpeedSection访问它,而不是直接这样做。然后你可以像现在一样保存它。 - Sudet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接