Save changes for a custom configuration section

  • Thread starter Giulio Petrucci
  • Start date
G

Giulio Petrucci

Hi there,

I've defined a 'Foo' configuration section in my App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Foo" type="FooLib.FooConfigSection, FooLib"></section>
</configSections>
<Foo label="Foo Label"></Foo>
</configuration>

....and defined a custom ConfigSection class:

public class FooConfigSection : ConfigurationSection
{
[ConfigurationProperty("label", IsRequired=true)]
public string Label
{
get { return (string)base["label"]; }
set { base["label"] = value; }
}
public override bool IsReadOnly() { return false; }
}

....which is used in the following code:

FooConfigSection fcs =
(FooConfigSection)ConfigurationManager.GetSection("Foo");
fcs.Label = "*";
Configuration c =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
c.Save(ConfigurationSaveMode.Full);

*but* nothing happens to the config file.
Could anyone help me?

Thanks in advance,
Giulio
--
 
G

Giulio Petrucci

Hi there,

Giulio said:
FooConfigSection fcs =
(FooConfigSection)ConfigurationManager.GetSection("Foo");
fcs.Label = "*";
Configuration c =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
c.Save(ConfigurationSaveMode.Full);

Solved:

FooConfigSection fcs =
(FooConfigSection)ConfigurationManager.GetSection("Foo");
fcs.SectionInformation.ForceSave = true; //missing line. :)
fcs.Label = "*";
Configuration c =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
c.Save(ConfigurationSaveMode.Full);

HTH,
Giulio
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top