[英]How to modify the App.Config section value
我有一个像下面的app.config,
<configuration>
<environment>
<add key="security" value="1"/> -- I want to change this value to 3
</environment>
</configuration>
我试着去下面的环境部分,
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
var environment = config.GetSection("environment");
环境变量没有给我足够的选项来让子元素修改值。 有人可以帮我这个忙吗?
使用用户范围设置!! 永远不要以这种方式改变应用程序配置。 应用程序中更改的任何值都应该是用户设置。
通常,您可以通过访问这些设置
Properties.Settings.Default.MyConfigurationValue = ....;
Properties.Settings.Default.Save();
编辑
做我在评论中写的示例。 创建两个用户设置: FirstRun
是一个bool
,默认情况下设置为true
。 Environment
是您的值,默认设置为0
。
然后,例如在Program.cs
的Main
函数中,您将执行以下操作:
if (Properties.Settings.Default.FirstRun)
{
Properties.Settings.Default.FirstRun = false;
if (myConditionIsTrue)
Properties.Settings.Default.Environment = 3;
Properties.Settings.Default.Save();
}
稍后在您的应用程序中使用Properties.Settings.Default.Environment
就足够了。 如果要从应用程序更改配置值,那么就是如何使用设置机制。
在Windows 2000,XP,7和Windows Server分支下,您甚至都没有权限修改Program Files文件夹中的app.config,所以请不要!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.