相关疑难解决方法(0)

在外部文件中编写appSettings

我有一个配置文件app.exe.config和appSettings部分有这样的:

<configuration>
    <appSettings configSource="app.file.config" />
</configuration>
Run Code Online (Sandbox Code Playgroud)

app.file.config文件有这样的东西:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="var1" value="value 1" />
  <add key="var2" value="value 2" />
  <add key="var3" value="value 3" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

我需要在运行时编辑var1,var2和var3,我有这样的代码:

Configuration config = ConfigurationManager.OpenExeConfiguration("...path\app.exe);

config.AppSettings.SectionInformation.ConfigSource = "app.file.config";

config.AppSettings.Settings["var1"].Value = "value 11";
config.AppSettings.Settings["var2"].Value = "value 22";
config.AppSettings.Settings["var3"].Value = "value 33";
config.Save(ConfigurationSaveMode.Modified);

ConfigurationManager.RefreshSection("appSettings");
Run Code Online (Sandbox Code Playgroud)

当我运行config.Save ....文件app.file.config有一个appSettings节点,其属性为"file".此属性具有app.file.config的值

<appSettings file="app.file.config">
<add key="var1" value="value 1" />
  <add key="var2" value="value 2" />
  <add key="var3" value="value 3" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

现在,如果我尝试加载配置文件,我有一个例外,消息"无法识别的属性'文件'.请注意,属性名称区分大小写." 在app.file.config中.

如果手动删除文件属性,则会正确加载配置文件.

有任何想法吗?

保存配置文件时如何避免写入文件属性.

谢谢

.net c# config

9
推荐指数
3
解决办法
1万
查看次数

标签 统计

.net ×1

c# ×1

config ×1