如何修改App.Config部分值
我有一个如下所示的 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");
环境变量没有给我足够的选项来获取子元素来修改值。任何人都可以帮我解决这个问题吗?
I have an app.config like below,
<configuration>
<environment>
<add key="security" value="1"/> -- I want to change this value to 3
</environment>
</configuration>
I tried like below to get to environment section,
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
var environment = config.GetSection("environment");
environment variable doesn't give me enough options to get the child elements to modify the value. Could any one please help me out in this one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用用户范围设置!切勿以这种方式更改应用程序配置。应用程序内更改的任何值都应该是用户设置。
访问这些设置
通常,您可以通过编辑
执行我在评论中所写内容的示例。创建两个用户设置:
FirstRun
是一个bool
,默认设置为true
。Environment
是您的值,默认设置为0
。然后,例如在
Program.cs
的Main
函数中,您将执行以下操作:稍后在应用程序中使用
Properties.Settings.Default 就足够了.环境
。如果您想更改应用程序中的配置值,这就是设置机制的用途。在 Windows 2000、XP、7 和 Windows Server 分支下,您甚至无权修改 Program Files 文件夹中的 app.config,所以不要这样做!
Use user scope settings!! NEVER EVER change the application configuration that way. Any value that is changed within the application should be a user setting.
Usually, you access these settings through
EDIT
Sample for doing what I wrote in the comments. Create two user settings:
FirstRun
is abool
which is by default set totrue
.Environment
is your value, by default set to0
.Then, for example in the
Main
function inProgram.cs
you'd do the following:Later in your application it is enough to use
Properties.Settings.Default.Environment
. That's how the settings mechanism is intended to be used if you want to change configuration values from your application.Under Windows 2000, XP, 7 and the Windows Server branch you would not even have the rights to modify the app.config in your Program Files folder, so don't!