如何在运行时修改我的 App.exe.config 键? [英] How to modify my App.exe.config keys at runtime?

查看:35
本文介绍了如何在运行时修改我的 App.exe.config 键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 app.config 我有这个部分

<add key ="UserId" value ="myUserId"/>//其他几个<添加键></appSettings>

通常我使用 userId = ConfigurationManager.AppSettings["UserId"]

访问这些值

如果我使用 ConfigurationManager.AppSettings["UserId"]=something 修改它,该值不会保存到文件中,下次我加载应用程序时,它会使用旧值.

如何在运行时更改某些 app.config 键的值?

解决方案

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);config.AppSettings.Settings["UserId"].Value = "myUserId";config.Save(ConfigurationSaveMode.Modified);

您可以在此处阅读有关 ConfigurationManager 的信息>

In my app.config I have this section

<appSettings>
    <add key ="UserId" value ="myUserId"/>
     // several other <add key>s
</appSettings>

Usually I access the values using userId = ConfigurationManager.AppSettings["UserId"]

If I modify it using ConfigurationManager.AppSettings["UserId"]=something, the value is not saved to the file, and next time I load the application, it uses the old value.

How can I change the value of some app.config keys during runtime?

解决方案

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["UserId"].Value = "myUserId";     
config.Save(ConfigurationSaveMode.Modified);

You can read about ConfigurationManager here

这篇关于如何在运行时修改我的 App.exe.config 键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆