更新 WPF 应用程序中的应用程序设置
我正在尝试使用下面的代码更新 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
configApp.AppSettings.Settings.Count.ToString()
这将尝试从
部分读取设置,而不是
。文件的名称也应该是app.config
。在您的情况下,您需要使用
Properties.Settings
静态类,从applicationSettings
访问您的设置。您可以尝试PontajWPF.Properties.Settings.Default.PontajAdminPwd
请阅读 MSDN
了解 更多信息这有帮助。
configApp.AppSettings.Settings.Count.ToString()
this will try to read settings from<appSettings>
section, not<applicationSettings>
. Also the name of the file shouldapp.config
.In your case you will need to use
Properties.Settings
static class, to access your settings fromapplicationSettings
. You can tryPontajWPF.Properties.Settings.Default.PontajAdminPwd
Read more on MSDN
Hope this helps.