【发布时间】:2012-11-30 18:55:33
【问题描述】:
我正在尝试为服务人员创建一个简单的工具来更新不同程序的 App.Config 中的一些条目。 App.Config 文件包含在我们的程序初始化时使用的自定义参数。
由于 App.Config 包含许多敏感项目,因此需要一个工具来确保仅更改某些参数。因此,不允许他们直接编辑 App.Config 的原因。
我的问题:
- 如何从单独的程序中访问 App.config 的配置部分中的名称-值对?
- 哪个更适合 UI:Winforms 还是 WPF?他们的控件是否便于将来添加更多条目?
- 该工具应允许用户设置 String、int、double 或 Boolean。
这里是 App.Config 的结构:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="Settings">
<section name="Section1" type="System.Configuration.NameValueSectionHandler"/>
<section name="Section2" type="System.Configuration.NameValueSectionHandler"/>
<section name="Section3" type="System.Configuration.NameValueSectionHandler"/>
<section name="Section4" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<Settings>
<Section1>
<add key="NAME_STRING" value="Some String"/>
</Section1>
<Section2>
<add key="NAME_INTEGER" value="10"/>
</Section2>
<Section3>
<add key="NAME_DOUBLE" value="10.5"/>
</Section3>
<Section4>
<add key="NAME_BOOLEAN" value="true"/>
</Section4>
</Settings>
... Omitted ...
</configuration>
在使用 App.Config 本身的程序中,我可以像这样轻松更改值:
NameValueCollection nvc = (NameValueCollection)ConfigurationManager.GetSection("Settings/Section1");
在加载 App.Config 后,是否有类似的方法可以从单独的程序中执行此操作?
【问题讨论】:
-
请注意:您的问题 3 实际上并不是一个问题。关于设置这些类型的数据,您有什么想问的吗?
-
啊,是的,很好!只是 UI 控件应该尽可能提供数据类型。如果有错误控制,例如在应该有整数的地方设置双精度,则会导致错误。
-
您是否考虑过加密敏感部分?然后您可以让服务人员更改未加密的部分。这个link 有更多信息。
-
感谢@DeanOC 的链接。下次我会注意这一点。不幸的是,目前我们无法更改 App.Config 程序。
标签: c# wpf winforms app-config