0

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.

Mayank Tripathi
  • 809
  • 1
  • 7
  • 11
  • Does the code snippet above is part of your wix# *building* code (which is executed on the developer machine when you building msi) or is a part of *installer* code (e.g. custom action), which is executed on the client machine during the installation? – Serg May 21 '21 at 14:22
  • This code is part of my wix# code. (Building code) Actually the variable to store location is getting set at compile time but i want to set it at runtime. – Mayank Tripathi May 23 '21 at 09:30

2 Answers2

2

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.

Serg
  • 3,454
  • 2
  • 13
  • 17
  • Thank you so much for the answer. I was bugging myself since 2 days and with this it is done in 5 minutes. – Mayank Tripathi May 23 '21 at 13:56
  • I used this and trying to create shortcut like below code but it is not working. Can you please suggest? , new Dir(@"%Desktop%", new ExeFileShortcut(new Id("DIR1"), "TestShortcut", "[root1]test.exe", arguments: "")) – Mayank Tripathi Jul 21 '21 at 19:27
  • Posted a separate question for this. https://stackoverflow.com/q/68475642/10281239 – Mayank Tripathi Jul 21 '21 at 19:49
0

I see that you're grabbing the environment variable in the User context. Have you tried it with the context set to Machine or Process? The installExecuteSequence that does the actual install runs in the context of System. I'm guessing this might fix this for you.

Doc
  • 698
  • 3
  • 16