oleg-shilo / wixsharp

Framework for building a complete MSI or WiX source code by using script files written with C# syntax.
MIT License
1.12k stars 175 forks source link

How to get Features installed path. #1545

Closed dorlep closed 4 months ago

dorlep commented 5 months ago

My Msi is installing features in separate folders and during uninstall I wanted to delete all the folders. How can I get the path of each features where that feature has installed in Msi_AfterInstall event, so that I can delete it Programmatically. Below is my code to create project

Feature pccwinFeature = new Feature("Job Worker Service", "Job worker", "FEATURE_INSTALL_PATH1");
Feature pccwebFeature = new Feature("PCC Portal", "Web Api", "FEATURE_INSTALL_PATH2");
Feature pModels = new Feature("Workflow Models", " Models", "FEATURE_INSTALL_PATH4");
Feature pIpc = new Feature("IPC Tomcat", " IPC Tomcat", "FEATURE_INSTALL_PATH3");

var project = new ManagedProject(productName,
          //PCC windows service
          new Dir(new Id("FEATURE_INSTALL_PATH1"), installDir,
               new Dir(windowsFolder,
              new Files(pccwinFeature, windowsSourceDir + @"\*.exe"))),
          //PCC Web
          new Dir(new Id("FEATURE_INSTALL_PATH2"), installDir,
               new Dir(wedFolder,
              new Files(pccwebFeature, webApiSourceDir + @"\*.dll"))),
         //Prophet Models
         new Dir(new Id("FEATURE_INSTALL_PATH4"), installDir,
             new Dir(prophetModelsFolder,
             new Files(pModels, pModelsSourceDir + @"\*.dll"))),
         new Dir(new Id("FEATURE_INSTALL_PATH3"), installDir,
             new Dir("Prophet IPC Tomcat",
            new Files(pIpc, iPCTomcatSourceDir + @"\*.*"))),
        new RegValue(pccwinFeature, WixSharp.RegistryHive.LocalMachine, "Software[\\PS\\P](file://ps/P)\\PCCWindows", "InstalledDirectory", "[FEATURE_INSTALL_PATH1]"),
        new RegValue(pccwebFeature, WixSharp.RegistryHive.LocalMachine, "Software\\PS\\P\\PC", "InstalledDirectory", "[FEATURE_INSTALL_PATH2]"),
        new RegValue(pIpc, WixSharp.RegistryHive.LocalMachine, "Software\\PS[\\P\\IPCWeb](file://p/IPCWeb)", "InstalledDirectory", "[FEATURE_INSTALL_PATH3]"),
       new RegValue(ipcTom, WixSharp.RegistryHive.LocalMachine, "Software\\PS[\\P\\IPCTomWeb](file://p/IPCTomWeb)", "InstalledDirectory", "[FEATURE_INSTALL_PATH4]"));

Please help me. Thank you in advance.

oleg-shilo commented 5 months ago

In the Msi_AfterInstall you can get the installdir from the args: e.InstallDir

Thus by combining it with the feature path you can get the exact dir:

var path = e.InstallDir.PathCombine("FEATURE_INSTALL_PATH2");

. . .

Unrelated. You may improve the readability of your code if you include it in the code section. It's not obvious so sharing it with you: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks

I have fixed your message above.

dorlep commented 5 months ago

Hi oleg-shilo,

Not getting the correct path, I am getting path like "C:\Temp\xyz\FEATURE_INSTALL_PATH2" which is like combine 1st feature path and then ID. Even I am getting only 1st feature path using e.InstallDir . How can I get the other features path as my all features are installed in different location (like one is on F drive and one is on c drive, parent dir is not same).

oleg-shilo commented 5 months ago

Sorry. There are two problems with the code I suggested:

I meant to resolve the FEATURE_INSTALL_PATH2 property with the session object. I got confused with what that string was in your code.

And also... InstallDir is unnecessary in the code I shared. The whole property/dirId is set by the MSI to the complete absolute path.

This is the code you need to use. I checked it. It works:

project.DefaultDeferredProperties += ",FEATURE_INSTALL_PATH2";
project.AfterInstall += (SetupEventArgs e) =>
{
    try
    {
        MessageBox.Show(e.Session.Property("FEATURE_INSTALL_PATH2"));
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
};

And... image

Note, project.DefaultDeferredProperties += ",FEATURE_INSTALL_PATH2"; without it, the AfterInstall cannot access this property as it is executed as deferred action and can only access deferred props. Don't ask, it is one of those black magic MSI cases.

dorlep commented 4 months ago

It is working. Thank you so much for your help :)