After most of the day researching, I am still unable to determine why the following code does not work as expected.
bool complete = false;
...
Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
BatchCompiler bc = new BatchCompiler(cfg.AppSettings.Settings);
... do stuff with bc ...
// Store the output of the operation.
BatchCompilerConfiguration bcc = (BatchCompilerConfiguration)ConfigurationManager.GetSection("BatchCompiler");
bcc.FilesCopied = complete;
bcc.OutputPath = bc.OutputPath;
cfg.Save(); // This does not write the modified properties to App.Config.
//cfg.SaveAs(@"c:\temp\blah.config") // This creates a new file Blah.Config with the expected section information, as it should.
The definition of the BatchCompilerConfiguration:
public sealed class BatchCompilerConfiguration : ConfigurationSection
{
public BatchCompilerConfiguration()
{
}
public override bool IsReadOnly()
{
return false;
}
[ConfigurationProperty("filesCopied", DefaultValue = "false")]
public bool FilesCopied
{
get { return Convert.ToBoolean(base["filesCopied"]); }
set { base["filesCopied"] = value; }
}
[ConfigurationProperty("outputPath", DefaultValue = "")]
public string OutputPath
{
get { return Convert.ToString(base["outputPath"]); }
set { base["outputPath"] = value; }
}
}
Here are the relevant sections from the App.Config:
<configSections>
<section name="BatchCompiler" type="BatchCompiler.BatchCompilerConfiguration, BatchCompiler" />
</configSections>
<BatchCompiler filesCopied="false" outputPath="" />
I've looked at http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx, the relevant MSDN articles and references for ConfigurationManager, and several existing questions here including:
- Custom Configuration in .Net
- Reload configuration settings...
- Problem implementing Custom Configuration...
- and several others.
I wouldn't expect to have to write a full custom element implementation to store the data I'm trying to store. However, if that's the only way to ensure the updated information is written to the App.Config file, I will write one. Please take a look and let me know what I've missed.