如何在卸载时通过Wix删除生成的文件夹和文件?

时间:2016-03-03 08:00:51

标签: c# asp.net-mvc wix .net-4.5 wix3.9

安装后,我在文件夹C:\Program Files (x86)中有以下文件夹结构:

Folder Structure after install

文件夹generated的路径为:C:\Program Files (x86)\CompanyName\AppName\generated

文件夹generated包含应用程序在运行时通过C#代码创建的子文件夹和文件:

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

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

变量lPathToDir可以包含以下值:

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

然后它看起来像:

enter image description here

我的问题:卸载后,这些子文件夹user1user2将不会被删除。我使用以下Wix声明:

<!-- 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>

卸载后:

enter image description here

为什么这些文件夹会保留,如何从安装文件夹中删除这些生成的文件夹?当我通过C#创建这些文件夹时,我可能需要设置任何权限吗?

2 个答案:

答案 0 :(得分:4)

根据Bob Arnson的建议,现在使用RemoveFolderEx。但除了声明<util:RemoveFolderEx On="uninstall" Property="APPLICATIONFOLDER" />之外,还需要一些注册表声明:

<!-- 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>

答案 1 :(得分:2)

在WixUtilExtension中使用RemoveFolderEx。我在介绍它的时候写了on my blog