无法保存对配置文件的更改

时间:2012-01-21 21:31:59

标签: c# app-config configurationmanager configurationsection

我一直关注this Stackoverflow topic。一切都在阅读方面起作用。我得到了一个集合,我将其转换为部分内容的字典。我将此发布到其他项目。当我尝试保存修改的节条目时出现问题。其中一个外部项目发送一个键/值对,我需要将它保存到我的部分。这是我的App.Config

     <?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并找不到任何更改。我错过了一些东西,可以用一些帮助搞清楚什么。感谢。

1 个答案:

答案 0 :(得分:3)

appConfiguration.Save(ConfigurationSaveMode.Modified, true);

appConfiguration.Save(ConfigurationSaveMode.Full, true);

后者应该强制完全保存配置文件,我认为前者也是如此。 我注意到你正在重新添加相同的密钥给你收集,可能这就是为什么它没有被修改?