views:

527

answers:

1

Hi,

I have the following section in my app.config

<mySettings>
        <addins>
            <addin fullname="aaaaa"
                   path="ddddd"
                   version="ccccc"
                   enabled="true"
                   deleteonload="false" />
        </addins>
    </mySettings>

And im accessing it using a class that derives from ConfigurationSection class. I have the read operations working well, however, what If I wanted to add another addin node via code?

I've managed to figure out how to write values to a config file other than the .exe config which is what i want to do, but every time I call .Save() instead of .SaveAs() I can't see the changes in the exe.config file. HELP!

AddInElement aelement = new AddInElement();
aelement.FullName = "asdasdasDASdasdzxv";

Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            MySettings mw = (MySettings)c.Sections["mySettings"];

            mw.SectionInformation.ForceSave = true;

            mw.AddInCollection.Add(aelement);

            c.SaveAs("yer.config", ConfigurationSaveMode.Full);




public class MySettings : ConfigurationSection {

        public static MyWaveSettings GetConfig() {
            return ConfigurationManager.GetSection("mySettings") as MySettings;
        }

        [ConfigurationProperty("addins", IsDefaultCollection = true)]
        public AddInElementCollection AddIns {
            get { return (AddInElementCollection)base["addins"]; }
        }
    }

    public sealed class AddInElementCollection : ConfigurationElementCollection {

        protected override ConfigurationElement CreateNewElement() {
            return new AddInElement();
        }

        protected override object GetElementKey(ConfigurationElement element) {
            return ((AddInElement)element).FullName;
        }

        public override ConfigurationElementCollectionType CollectionType {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }

        protected override string ElementName {
            get { return "addin"; }
        }

        public AddInElement this[int index] {
            get { return (AddInElement)BaseGet(index); }
        }
    }

    public class AddInElement : ConfigurationElement {

        [ConfigurationProperty("fullname", IsRequired = true)]
        public String FullName {
            get { return (String)this["fullname"]; }
            set { this["fullname"] = value; }
        }

        [ConfigurationProperty("path", IsRequired = true)]
        public String Path {
            get { return (String)this["path"]; }
            set { this["path"] = value; }
        }

        [ConfigurationProperty("version", IsRequired = true)]
        public String Version {
            get { return (String)this["version"]; }
            set { this["version"] = value; }
        }

        [ConfigurationProperty("enabled", IsRequired = true)]
        public Boolean Enabled {
            get { return (Boolean)this["enabled"]; }
            set { this["enabled"] = value; }
        }

        [ConfigurationProperty("deleteonload", IsRequired = true)]
        public Boolean DeleteOnLoad {
            get { return (Boolean)this["deleteonload"]; }
            set { this["deleteonload"] = value; }
        }
    }
A: 

The .exe.config file is just a template for the applications settings. When you change settings within the user scope at runtime, a new .exe.config file is saved in %APPDATA% (or System.Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData );). Try to check whether your changed settings are in this directory.

PVitt