How can I delete generated folders and files via Wix on uninstall?
Asked Answered
C

2

10

After an installation, I have the following folder structure in the folder C:\Program Files (x86):

Folder Structure after install

The Path to the folder generated is: C:\Program Files (x86)\CompanyName\AppName\generated

The folder generated contains subfolders and files they will be created by the application during the runtime via C# code:

var lPathToDir = Path.Combine(lFileService.GetFilePath, pSamAccountName);

if (!Directory.Exists(lPathToDir))
{
    Directory.CreateDirectory(lPathToDir);
}

The variable lPathToDir could have the values:

C:\Program Files (x86)\CompanyName\AppName\generated\user1
// or
C:\Program Files (x86)\CompanyName\AppName\generated\user2

Then it looks like:

enter image description here

My Problem: After an uninstall, these subfolders user1, user2 will not be removed. I use the following Wix declaration:

<!-- Target installation folder -->
<Directory Id="ProgramFilesFolder" Name="$(var.ProgramFilesFolder)">
    <Directory Id="APPLICATIONFOLDER" Name="$(var.AppFolderName)">

      <Directory Id="BIN" Name="bin" />
      <Directory Id="HELP" Name="help" />
      <Directory Id="GENERATED" Name="generated" />

      <Component Id="RemoveAll" Guid="THE-GUID-HERE">
        <RemoveFile Id="RemoveAllFilesOnUninstall" Directory="APPLICATIONFOLDER" Name="*.*" On="uninstall" />
        <RemoveFolder Id="RemoveAllFoldersOnUninstall" Directory="APPLICATIONFOLDER" On="uninstall" />
        <util:RemoveFolderEx On="uninstall" Property="GENERATED" />
      </Component>

    </Directory>
</Directory>

After uninstall:

enter image description here

Why do these folders stay and how can I remove these generated folders from the installation folder? Perhaps do I need to set any permissions when I create these folders via C#?

Candi answered 3/3, 2016 at 8:0 Comment(3)
This thread should help you. Specifically, the top 2 top-rated answers.Flanigan
@Yan Sklyarenko: As you can see, I already use the solution of the best ranked answer with no success.Candi
Ah, right, missed that.Flanigan
C
6

Works now with RemoveFolderEx as suggested by Bob Arnson. But in addition to the declaration <util:RemoveFolderEx On="uninstall" Property="APPLICATIONFOLDER" /> there are also some registry declarations necessary:

<!-- add this: -->
<Property Id="APPLICATIONFOLDER">
  <RegistrySearch Key="SOFTWARE\$(var.Manufacturer)\$(var.AppName)" Root="HKLM" Type="raw" Id="APPLICATIONFOLDER_REGSEARCH" Name="Path" />
</Property>

  <Directory Id="BIN" Name="bin" />
  <Directory Id="HELP" Name="help" />
  <Directory Id="GENERATED" Name="generated" />

  <Component Id="RemoveAll" Guid="THE-GUID-HERE">
    <RemoveFile Id="RemoveAllFilesOnUninstall" Directory="APPLICATIONFOLDER" Name="*.*" On="uninstall" />
    <RemoveFolder Id="RemoveAllFoldersOnUninstall" Directory="APPLICATIONFOLDER" On="uninstall" />

    <!-- add this: -->
    <RegistryValue Root="HKLM" Key="SOFTWARE\$(var.Manufacturer)\$(var.AppName)" Name="Path" Type="string" Value="[APPLICATIONFOLDER]" KeyPath="yes" />

    <util:RemoveFolderEx On="uninstall" Property="APPLICATIONFOLDER" />
  </Component>

</Directory>

Candi answered 7/3, 2016 at 14:3 Comment(0)
I
4

Use RemoveFolderEx in WixUtilExtension. I wrote about it on my blog back when I introduced it.

Itemized answered 3/3, 2016 at 17:32 Comment(1)
Thank you very much for your answer. I have added RemoveFolderEx to my declaration, but the folder GENERATED still remains.Candi

© 2022 - 2024 — McMap. All rights reserved.