radbyx
radbyx

Reputation: 9660

Saving to web.config, adds an extra "config" extension

I'm trying to build a web.config file, in Advanced Installer .MSI Wizard, with the Users Connection String input data.

Why does this result in a file web.config.config with an extra config and how do I avoid it?

I guess my Open or Save isn't right?

It's a Custom Action, inside a .MSI, I ran from Advanced Installer, but it shouldn't have any impact I think.

[CustomAction]
public static ActionResult EncryptConnStr(Session session)
{
    try
    {
        var path = Path.Combine(@"C:\Users\radbyx\Documents", "web.config");
        var connStr = BuildConnStr("foo", "foo", "foo", "foo");

        Configuration config = ConfigurationManager.OpenExeConfiguration(path);
        ConnectionStringsSection section = (ConnectionStringsSection)config.GetSection("connectionStrings");
        section.ConnectionStrings.Add(new ConnectionStringSettings(GetConnectionStringName(), connStr));

        // Encrypt
        //section.SectionInformation.ProtectSection(ConnStrEncryptionKey);

        // Save the configuration file.
        config.Save(ConfigurationSaveMode.Modified, true);

        return ActionResult.Success;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + "Trace: " + ex.StackTrace, ex.Message);
        throw;
    }
}

Upvotes: 0

Views: 279

Answers (1)

Paul Suart
Paul Suart

Reputation: 6713

This behaviour is caused by ConfigurationManager.OpenExeConfiguration expecting you to provide the path to an executable, not a config file.

To open a config file explicitly, use the overload that takes a map:

var map = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
configuration = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

Upvotes: 2

Related Questions