Search code examples
wixwixsharp

Provide the Installation directory location at runtime in wix sharp


I am using wix sharp to develop my installer and everything is working fine except I'm unable to provide the install directory location at run time. While installation, I'm taking input from user and storing them in environment variable as "InstallFolder" and in wix sharp code I'm taking this from Environment variable.

 var installLocation = Environment.GetEnvironmentVariable("installLocation", 
 EnvironmentVariableTarget.User);
 var XYZ_project = new ManagedProject("xyz_Product",
 new Dir(installLocation,new Files(@"xxx\yyy\*.*"))

Ideally it should take the instalLocation from env variable but it is not taking it at runtime. If I set this value before building the installer itself it is taking the value.

I need to get the values from user on runtime and set them. Please suggest on this.


Solution

  • To set install dir at runtime, you can use ManagedProject.Load event.

    In the project declaration set the root directory ID ("DIR1" in the sample) and subscribe to the Load event.

      var project =
            new ManagedProject("MyProduct",
                new Dir(new Id("DIR1"), "root1", new File("test.exe")));
    
        project.Load += Project_Load;
    

    In event handler set the value for the dir

    static void Project_Load(SetupEventArgs e)
    {
        e.Session["DIR1"] = 
    Environment.GetEnvironmentVariable("installLocation", 
     EnvironmentVariableTarget.User);
        }
    

    The Project_Load will be called on client machine before the installation, but after all user input collected.

    See full sample here and documentation about wix# event here.