开发者

Updates to custom configuration section do not write to app.config

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 exi开发者_开发百科sting 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.


If a google search brings you to this question, note this:

ConfigurationManager.GetSection("BatchCompiler") provides an instance of BatchCompiler with properties set to the DefaultValue of the custom attributes of class BatchCompiler.

However, it is readonly. If you think about it, this makes sense. You've not told ConfigurationManager which file to use, so how could you persist the changes?

BatchCompilerConfiguration allows read/write, because of a short cut in implementation. The orginal poster should not allow values to be set if the inherited IsReadOnly method returns true.

To get a read/write section, use

BatchCompilerConfiguration sectionconfig =(BatchCompilerConfiguration)ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).Sections["BatchCompiler"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜