JKennedy
JKennedy

Reputation: 18827

Is there a way to write to a Custom app.Config Configuration from code

So far I have:

<section name="PinnedPhotos" type="PhotoViewer.PinnedPhotos, PhotoViewer" allowLocation="true" allowDefinition="Everywhere"/>
<PinnedPhotos>
  <add location="Location1.jpg"/>
  <add location="Location2.jpg"/>
</PinnedPhotos>

in my app.Config

and in my Code I have:

 PinnedPhotos config = (PinnedPhotos)System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location).Sections["PinnedPhotos"];

But I would like to update and save config from code e.g. Adding more photos to be pinned to the existing list. Is there a way to do this?

EDIT

var isReadOnly = config.Photos.IsReadOnly();

returns False... So I guess there should be a way to write to this collection?

Upvotes: 0

Views: 49

Answers (1)

JKennedy
JKennedy

Reputation: 18827

Got it: I needed to add the two Methods to my Collection:

    public void Add(string location)
    {
        PhotoElement pe = new PhotoElement(location);
        BaseAdd(pe);
    }
    public void Remove(string location)
    {
        BaseRemove(location);
    }

and then It was just a Case of calling:

config.CurrentConfiguration.Save();

Upvotes: 1

Related Questions