2

I am totally new to Wix and wix#, I am trying to update the app.config after the installation is finished. I am able to achieve it by using the Wix Util extension util:XmlFile, but I want it to be done by wix# CustomDialog UI.

Below is the code which I had tried

var project = new ManagedProject("MyProduct",
                             new Dir(@"%ProgramFiles%\My Company\My Product",
                                 new File("Program.cs"),
                                 new File(@"myPath\App.config")),
                              new ElevatedManagedAction(CustomActions.OnInstall, Return.check, When.After, Step.InstallFiles, Condition.NOT_Installed)
                              {
                                  UsesProperties = "CONFIG_FILE=[INSTALLDIR]App.config"
                              });

        project.Load += Msi_Load;
        project.BeforeInstall += Msi_BeforeInstall;
        project.AfterInstall += Msi_AfterInstall;

Created a CustomDialog and set its value to a session variable after next

void next_Click(object sender, EventArgs e)
    {
        MsiRuntime.Session["NAME"] = name.Text;
        Shell.GoNext();
    }

I am able to retrieve session value in Msi_BeforeInstall but here app.config path is getting null as it is not copied to the INSTALLDIR and when I tried to perform it on Msi_AfterInstall here I am not getting the session variable property

I also tried to do it by CustomAction after installation

 [CustomAction]
    public static ActionResult OnInstall(Session session)
    {

        return session.HandleErrors(() =>
        {
            string configFile = session.Property("INSTALLDIR") + "App.config";
            string userName = session.Property("NAME");
            UpdateAsAppConfig(configFile, userName);
        });
    }
    static public void UpdateAsAppConfig(string configFile,string name)
    {
        var config = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = configFile }, ConfigurationUserLevel.None);

        config.AppSettings.Settings["MyName"].Value = name;

        config.Save();
    }

But not getting the session variable property. I am really new to it, any help would be appreciated. Please help me if I am doing it wrong or how can I update my app.config after installation.

Thanks.

DevProf
  • 794
  • 2
  • 9
  • 20
  • No time to answer, but [have a quick look at Painter's answer here](https://stackoverflow.com/a/49142733/129130). [Might be more here](https://stackoverflow.com/questions/27691636/write-appsettings-in-external-file). – Stein Åsmul Apr 24 '20 at 01:00

1 Answers1

0

I know your problem, AfterInstall event works with dead session it's unaccessable at this moment. If you need some properties in AfterInstall moment you can do use SetupEventArgs.Data property:

    private void OnBeforeInstall(SetupEventArgs arguments)
    {
        //....
        arguments.Data["MyName"] = arguments.Session["MyName"];
    }

    private void OnAfterInstall(SetupEventArgs arguments)
    {
        var propertyValue = arguments.Data["MyName"];
    }

Also Data property can be used in your UI forms shown AFTER ProgressBarForm. Hope it help to you, let me know your feedback.

Sergey Vaulin
  • 722
  • 4
  • 16