是否可以以编程方式写入app.config文件中的<startup> <supportedruntime>设置?

时间:2016-02-18 11:24:46

标签: c# .net app-config

我正在为我们的部分产品编写一个小型“配置”库。我们的想法是以编程方式构建app.config文件(通常在安装时使用其他程序)。我希望能够在这个领域写一个新的设置(指定.Net 3.5,.Net 4.0等)有没有人知道是否可以通过System.Configuration ConfigurationManager / Configuration API?

我可以通过要求ConfigurationManager加载模板文件来获取Configuration对象,然后通过

获取相关部分
var configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = "C:\template.config";
Configuration myConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
var sec = myConfig.GetSection("startup") as IgnoreSection;

但是,如上所示,所有这些都会让你成为一个“IgnoreSection”,它似乎不是可变的或者可以转换为某些其他底层类型。

模板文件是一个非常空的配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  </configSections>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

我唯一的选择是将当前配置写入磁盘(感谢Configuration API,没有流!)然后用正则表达式或XML替换它?

0 个答案:

没有答案