关于c#:在WPF应用程序中更新应用程序设置

Updating application settings in WPF application

我正在尝试使用以下代码更新app.config文件中的值(该值在"属性">"设置"中定义为"应用程序范围")

1
2
3
4
System.Configuration.Configuration configApp = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MessageBox.Show(configApp.AppSettings.Settings.Count.ToString()); //this shows 0
configApp.AppSettings.Settings["PontajAdminPwd"].Value ="dsfs";
configApp.Save(ConfigurationSaveMode.Full);

但它说configApp.AppSettings.Settings为空...

这是我的app.config文件的一部分

1
2
3
4
5
6
    <PontajWPF.Properties.Settings>
        <setting name="PontajAdminPwd" serializeAs="String">
            <value>696W3oybVP85szuiY2Qpiw==</value>
        </setting>
    </PontajWPF.Properties.Settings>
</applicationSettings>

我究竟做错了什么?

谢谢

编辑1:我很着急,所以我采用了这里提出的解决方案(手动更改app.config文件后直接访问文件-使用appSettings代替applicationSettings):
http://www.longhorncorner.com/uploadfile/rahul4_saxena/update-app-config-key-value-at-run-time-in-wpf/


configApp.AppSettings.Settings.Count.ToString()这将尝试从部分而不是读取设置。 文件名也应为app.config

对于您的情况,您将需要使用Properties.Settings静态类,以从applicationSettings访问设置。 您可以尝试PontajWPF.Properties.Settings.Default.PontajAdminPwd

Application-scope settings are read only, and can only be changed at design time or by altering the .exe.config file in between application sessions.

User-scope settings, however, can be written at run time, just as you would change any property value. The new value persists for the duration of the application session. You can persist changes to user settings between application sessions by calling the Settings.Save method. These settings are saved in the User.config file.

阅读有关MSDN的更多信息

希望这可以帮助。