更新 WPF 应用程序中的应用程序设置

发布于 2025-01-03 09:26:55 字数 1208 浏览 0 评论 0原文

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

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 文件

<applicationSettings>
    <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/

I'm trying to update a value in my app.config file using the code below (the value is defined in Properties > Settings as Application scoped)

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

but it saying that configApp.AppSettings.Settings is empty...

This is a part of my app.config file

<applicationSettings>
    <PontajWPF.Properties.Settings>
        <setting name="PontajAdminPwd" serializeAs="String">
            <value>696W3oybVP85szuiY2Qpiw==</value>
        </setting>
    </PontajWPF.Properties.Settings>
</applicationSettings>

What am I doing wrong?

Thank you

EDIT 1: I'm in a hurry so I adopted the solution proposed here (direct file access after changing the app.config file by hand - using appSettings instead of applicationSettings):
http://www.longhorncorner.com/uploadfile/rahul4_saxena/update-app-config-key-value-at-run-time-in-wpf/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅一夜美梦 2025-01-10 09:26:55

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

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

应用程序范围设置是只读的,并且只能在设计时更改通过在应用程序会话之间更改 .exe.config 文件< /strong>.

但是,

用户范围设置可以在运行时写入,就像更改任何属性值一样。新值在应用程序会话期间持续存在。您可以通过调用 Settings.Save 方法在应用程序会话之间保留对用户设置的更改。这些设置保存在 User.config 文件中。

请阅读 MSDN

了解 更多信息这有帮助。

configApp.AppSettings.Settings.Count.ToString() this will try to read settings from <appSettings> section, not <applicationSettings>. Also the name of the file should app.config.

In your case you will need to use Properties.Settings static class, to access your settings from applicationSettings. You can try 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.

Read more on MSDN

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文