ConfigManager filename issue

My problem concerns using custom config files. When I load a config using 'System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("example.config");' the manager saves to "example.config.config". How can I stop it appending an additional ".config" to the filename

Answer this question

ConfigManager filename issue

  • NFox

    OpenExeConfiguration(string appName) is designed to work with application config files; a config file named specifically "appName.config". The OpenExeConfiguration code checks for the existance of "appName" as well as "appName.config".

    Because of this the following two lines will both open "MyApp.exe.config", but only if BOTH the exe *and* the config file exist.
    OpenExeConfiguration("MyApp.exe");
    OpenExeConfiguration("MyApp.exe.config");

    However, it doesn't check that MyApp.exe is actually a valid executable file. So if this is truly something you need, you can fool the system by creating a file (ANY file, text, zero-byte, whatever) - called "example". No extension, nothing.

    Now by calling
    OpenExeConfiguration("example");
    ...or
    OpenExeConfiguration("example.config");

    The system will work the way you seem to expect.


  • odklizec

    I have been unable to duplicate this. Are you calling (essentially) the following two lines of code...

    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("example.config");

    config.Save();

    In this case, Save doesn't create a new filename, it merely saves back to the file it previously opened. Is this not what is happening for you


  • CBerk

    Excellent. What I actually need is OpenExeConfiguration("example.dll"). this opens and saves to example.dll.config.

    Thanks for your help


  • neto

    There's a custom section bit between the open and save. You are correct that the open and save on there own do not produce the error, but with the custom section in place the filename goes bad:

    System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration("example.config");

    if (config.Sections["CustConfig"] == null)

    {

    CustConfigSection custSection = new CustConfigSection();

    config.Sections.Add("CustConfig", custSection);

    custSection = config.GetSection("CustConfig") as CustConfigSection;

    custSection.SectionInformation.ForceSave = true;

    }

    config.Save(ConfigurationSaveMode.Full);


  • ConfigManager filename issue