无法保存对配置文件的更改
本文关键字:配置文件 保存 | 更新日期: 2023-09-27 18:20:56
我一直在关注这个堆栈溢出主题。一切都在阅读方面起作用。我得到了一个集合,我把它转换成一个章节内容的字典。我将此发布到其他项目。当我试图保存修改后的节条目时,就会出现问题。其中一个外部项目发送了一个键/值对,我需要将其保存到我的部分。这是我的应用程序配置
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="fileEnvironmentSection" type="MyApp.Infrastructure.Configuration.FileEnvironmentSection, MyApp.Infrastructure"/>
</configSections>
<fileEnvironmentSection>
<fileEnvironment>
<add key="TestEntry1" value="A nice value"/>
<add key="TestEntry2" value="Another value"/>
</fileEnvironment>
</fileEnvironmentSection>
</configuration>
以下是与配置部分交互所需的三层类型。
using System.Configuration;
namespace SlamDunk.Infrastructure.Configuration {
public class FileEnvironmentSection : ConfigurationSection {
[ConfigurationProperty("fileEnvironment", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(FileEnvironmentCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
public FileEnvironmentCollection FileEnvironmentList {
get { return (FileEnvironmentCollection)base["fileEnvironment"]; }
}
}
}
using System.Configuration;
namespace SlamDunk.Infrastructure.Configuration {
public class FileEnvironmentCollection : ConfigurationElementCollection {
public FileEnvironmentElement this[int index] {
get { return (FileEnvironmentElement)BaseGet(index); }
set {
if(BaseGet(index) != null) BaseRemoveAt(index);
BaseAdd(index, value);
}
}
public void Add(FileEnvironmentElement fileEnvironmentElement) { BaseAdd(fileEnvironmentElement); }
public void Clear() { BaseClear(); }
protected override ConfigurationElement CreateNewElement() { return new FileEnvironmentElement(); }
protected override object GetElementKey(ConfigurationElement element) { return ((FileEnvironmentElement)element).Key; }
public void Remove(FileEnvironmentElement fileEnvironmentElement) { BaseRemove(fileEnvironmentElement.Key); }
public void Remove(string key) { BaseRemove(key); }
public void RemoveAt(int index) { BaseRemoveAt(index); }
}
}
using System.Configuration;
namespace SlamDunk.Infrastructure.Configuration {
public class FileEnvironmentElement : ConfigurationElement {
private const string appConfigDefaultString = "missing";
private const string _appConfigNameKey = "key";
private const string _appConfigNameValue = "value";
public FileEnvironmentElement() { }
public FileEnvironmentElement(string key, string value) {
Key = key;
Value = value;
}
[ConfigurationProperty(_appConfigNameKey, DefaultValue = appConfigDefaultString, IsRequired = true, IsKey = true)]
public string Key {
get { return (string)this[_appConfigNameKey]; }
set { this[_appConfigNameKey] = value; }
}
[ConfigurationProperty(_appConfigNameValue, DefaultValue = appConfigDefaultString, IsRequired = true, IsKey = false)]
public string Value {
get { return (string)this[_appConfigNameValue]; }
set { this[_appConfigNameValue] = value; }
}
}
}
这是我用来保存键/值更改的代码。
var appConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var fileEnvironmentSection = appConfiguration.GetSection("fileEnvironmentSection") as FileEnvironmentSection;
var fileEnvironmentList = fileEnvironmentSection.FileEnvironmentList;
fileEnvironmentList.Remove(key);
var element = new FileEnvironmentElement(key, value);
fileEnvironmentList.Add(element);
//appConfiguration.Save(ConfigurationSaveMode.Modified);
//fileEnvironmentSection.CurrentConfiguration.Save(ConfigurationSaveMode.Modified);
fileEnvironmentList.CurrentConfiguration.Save(ConfigurationSaveMode.Modified);
我已经检查过了,更改如预期的那样出现在列表中。我已经试过两次有评论的保存呼叫加上最后一次。我想我正在获得FileEnvironmentSection的一个新实例,以避免ConfigurationManager缓存问题。每次测试运行后,我都会查看MyApp.exe.config,但没有发现任何更改。我错过了一些东西,需要一些帮助才能弄清楚是什么。谢谢
appConfiguration.Save(ConfigurationSaveMode.Modified, true);
appConfiguration.Save(ConfigurationSaveMode.Full, true);
后者应该强制完整保存配置文件,我认为前者也会。我注意到你正在重新添加与你收集的密钥相同的密钥,这可能就是为什么它没有被修改的原因?