oleg-shilo / wixsharp

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

Uninstall / Delete Files or Dirs on Programm uninstallation #1489

Closed Torchok19081986 closed 2 months ago

Torchok19081986 commented 2 months ago

Morning, i try to add conditinally Question at Point of Uninstallation. Ask User if he want also delete all config Files and programmcreated Dirs. But somehow i dont get Question at Uninstall. My CA for this Pupose looks following


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

     try
     {

            var result = System.Windows.MessageBox.Show("Do you want also delete all config Files and Dirs ?", "Delete All Files", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

          if(result == MessageBoxResult.Yes)
          {         
             System.IO.DirectoryInfo di = new DirectoryInfo(session["INSTALLDIR"]);

             foreach (FileInfo file in di.GetFiles())
             {
                file.Delete(); 
             }
             foreach (DirectoryInfo dir in di.GetDirectories())
             {
               dir.Delete(true); 
             }
          } 
          return ActionResult.Success;
     }
     catch(Exception exc)
     {
         return ActionResult.Failure;
     }

I think , i use wrong entry point of CA. Where i should set for execution ? Step.RemoveFiles or Step.RemoveFolders not working.

oleg-shilo commented 2 months ago

This will do:


project.BeforeInstall += args =>
        {
            if (args.IsUninstalling)
            {
                   var result = System.Windows.MessageBox.Show("Do you ....);
                   if(result == MessageBoxResult.Yes)
                       session["INSTALLDIR"].DeleteIfExists(); // it will delete all files and subdirs
            }
         };

It will also convert the exception to the ActionResult.Failure.

Torchok19081986 commented 2 months ago

thanks, i just tried it. Works like expected. This is specific for all files exists in same path as Installdir. How do i also delete files or dirs if these not in installdir, for example UNC-Path or in UserPersonal Folder ?

oleg-shilo commented 2 months ago

The same way: "<path to your folder>".DeleteIfExists()