radbyx
Posts: 2
Joined: Mon Aug 14, 2017 12:55 pm

Create or Edit a file at Installation Time.

I have a ConnStrDlg, where the User types in Server, Database, Username, Password for he's Connection String. (See Attachment ConnStrDlg).


I have this code that works, that takes the 4 properties and make a connectionString element in the web.config and save it encrypted. But it only works on a hardcoded path.

How do I solve this, when I want to create or use a web.config that comes from AI? I've tryed creating a web.config in "Files and Folders", but my Custom Action looks for that file before it's in the APPDIR. The Custom Action Execution Time is set to Immediately and placed in Install Execution Stage between "Add Resources" and "Finish Execution" . I've also try'd a little with temp. files but with no luck.

I'm wondering if there is an easy fix, or I should try to delay my Custom Action to first run after the Installation Wizard is done, or maybe use a temp web.config file, I could modify and save to my APPDIR. Or maybe there are another simple path I should use at Installation time instead of APPDIR?

Code: Select all

[CustomAction]
public static ActionResult EncryptConnStr(Session session)
{
    try
    {
        var connStr = BuildConnStr(session["CONN_STR_SERVER"], session["CONN_STR_DATABASE"], session["CONN_STR_USERNAME"], session["CONN_STR_PASSWORD"]);

        var path = Path.Combine(@"C:\Users\radbyx\Documents", "web.config"); // <--- TODO Hardcoded path. 
        var map = new ExeConfigurationFileMap { ExeConfigFilename = path };
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

        ConnectionStringsSection section = (ConnectionStringsSection)config.GetSection("connectionStrings");

        // Add Connectionstrings element
        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;
    }
}
Attachments
ConnStrDlg.PNG
ConnStrDlg.PNG (21.35 KiB) Viewed 2043 times
radbyx
Posts: 2
Joined: Mon Aug 14, 2017 12:55 pm

Re: Create or Edit a file at Installation Time.

I solved this by
1) using a deferred Custom Action
2) seperate the public properties with a "|" seperator
3) Accessing the public properties with "CustomActionData" like this
var delimiter = '|';
var actionData = session["CustomActionData"];
var dataArray = actionData.Split(delimiter);
var server = dataArray[0];
var database = dataArray[1];
var username = dataArray[2];
var password = dataArray[3];
var path = Path.Combine(dataArray[4], "web.config");
var connStr = BuildConnStr(server, database, username, password);
Sorin
Posts: 663
Joined: Mon May 08, 2017 1:03 pm

Re: Create or Edit a file at Installation Time.

Hello and welcome to our forums,

I'm glad that you've sorted thing out. Thank you for posting your solution.

Should there be any difficulty implementing something, please let me know.

Best regards,
Sorin
Sorin Stefan - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Building Installers”