Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7087659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:42:05+00:00 2026-05-28T07:42:05+00:00

I’ve been following this Stackoverflow topic . Everything works on the read side. I

  • 0

I’ve been following this Stackoverflow topic. Everything works on the read side. I get a collection which I transform into a dictionary of the section contents. I publish this to other projects. The problems occur when I try to save a modified section entry. One of the external projects send a key/Value pair and I need to save it to my section. Here is my 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>

Here are the three layers of types needed to interact with the config section.

     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; }
          }
        }
     }

Here is the code I’m using to save a key/value change.

     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);

I’ve checked and the changes appear in the list as expected. I’ve tried the two commented save call plus the last one. I think I’m getting a new instance of FileEnvironmentSection to avoid ConfigurationManager caching issues. After each test run I look in MyApp.exe.config and do not find any changes. I’m missing something and could use some help figuring out what. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:42:06+00:00Added an answer on May 28, 2026 at 7:42 am
    appConfiguration.Save(ConfigurationSaveMode.Modified, true);
    
    appConfiguration.Save(ConfigurationSaveMode.Full, true);
    

    the latter should force a full save of the config file, i think the former will as well.
    i noticed that you are re-adding the same key to you collect, possibly that’s why it is not picked up as modified?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.