89

卸载我的应用程序时,我想配置Wix设置以删除原始安装后添加的所有文件。似乎卸载程序仅删除了最初从 MSI 文件安装的目录和文件,而将后来添加的所有其他内容保留在应用程序文件夹中。换句话说,我想在卸载时清除目录。我怎么做?

4

6 回答 6

87

使用带有 On=" uninstall " 的RemoveFile 元素。这是一个例子:

<Directory Id="CommonAppDataFolder" Name="CommonAppDataFolder">
  <Directory Id="MyAppFolder" Name="My">
    <Component Id="MyAppFolder" Guid="*">
      <CreateFolder />
      <RemoveFile Id="PurgeAppFolder" Name="*.*" On="uninstall" />
    </Component>
  </Directory>
</Directory>

更新

它没有 100% 有效。它删除了文件,但是没有删除任何附加目录(安装后创建的目录)。对此有什么想法吗?– 普里贝罗

不幸的是,Windows Installer 不支持删除带有子目录的目录。在这种情况下,您必须诉诸自定义操作。或者,如果您知道子文件夹是什么,请创建一堆 RemoveFolder 和 RemoveFile 元素。

于 2008-10-12T21:38:42.043 回答
32

在 WiX 中使用RemoveFolderExUtil 扩展中的元素。
使用这种方法,所有子目录也被删除(而不是直接使用RemoveFileelement)。此元素将临时行添加到MSI 数据库中RemoveFileRemoveFolder表。

于 2012-05-07T06:20:33.780 回答
13

为此,我只是创建了一个在卸载时调用的自定义操作。

WiX 代码将如下所示:

<Binary Id="InstallUtil" src="InstallUtilLib.dll" />

<CustomAction Id="DIRCA_TARGETDIR" Return="check" Execute="firstSequence" Property="TARGETDIR" Value="[ProgramFilesFolder][Manufacturer]\[ProductName]" />
<CustomAction Id="Uninstall" BinaryKey="InstallUtil" DllEntry="ManagedInstall" Execute="deferred" />
<CustomAction Id="UninstallSetProp" Property="Uninstall" Value="/installtype=notransaction /action=uninstall /LogFile= /targetDir=&quot;[TARGETDIR]\Bin&quot; &quot;[#InstallerCustomActionsDLL]&quot; &quot;[#InstallerCustomActionsDLLCONFIG]&quot;" />

<Directory Id="BinFolder" Name="Bin" >
    <Component Id="InstallerCustomActions" Guid="*">
        <File Id="InstallerCustomActionsDLL" Name="SetupCA.dll" LongName="InstallerCustomActions.dll" src="InstallerCustomActions.dll" Vital="yes" KeyPath="yes" DiskId="1" Compressed="no" />
        <File Id="InstallerCustomActionsDLLCONFIG" Name="SetupCA.con" LongName="InstallerCustomActions.dll.Config" src="InstallerCustomActions.dll.Config" Vital="yes" DiskId="1" />
    </Component>
</Directory>

<Feature Id="Complete" Level="1" ConfigurableDirectory="TARGETDIR">
    <ComponentRef Id="InstallerCustomActions" />
</Feature>

<InstallExecuteSequence>
    <Custom Action="UninstallSetProp" After="MsiUnpublishAssemblies">$InstallerCustomActions=2</Custom>
    <Custom Action="Uninstall" After="UninstallSetProp">$InstallerCustomActions=2</Custom>
</InstallExecuteSequence>

InstallerCustomActions.DLL 中 OnBeforeUninstall 方法的代码将如下所示(在 VB 中)。

Protected Overrides Sub OnBeforeUninstall(ByVal savedState As System.Collections.IDictionary)
    MyBase.OnBeforeUninstall(savedState)

    Try
        Dim CommonAppData As String = Me.Context.Parameters("CommonAppData")
        If CommonAppData.StartsWith("\") And Not CommonAppData.StartsWith("\\") Then
            CommonAppData = "\" + CommonAppData
        End If
        Dim targetDir As String = Me.Context.Parameters("targetDir")
        If targetDir.StartsWith("\") And Not targetDir.StartsWith("\\") Then
            targetDir = "\" + targetDir
        End If

        DeleteFile("<filename.extension>", targetDir) 'delete from bin directory
        DeleteDirectory("*.*", "<DirectoryName>") 'delete any extra directories created by program
    Catch
    End Try
End Sub

Private Sub DeleteFile(ByVal searchPattern As String, ByVal deleteDir As String)
    Try
        For Each fileName As String In Directory.GetFiles(deleteDir, searchPattern)
            File.Delete(fileName)
        Next
    Catch
    End Try
End Sub

Private Sub DeleteDirectory(ByVal searchPattern As String, ByVal deleteDir As String)
    Try
        For Each dirName As String In Directory.GetDirectories(deleteDir, searchPattern)
            Directory.Delete(dirName)
        Next
    Catch
    End Try
End Sub
于 2008-11-06T21:39:41.177 回答
12

这是@tronda 建议的变体。我正在删除在卸载期间由另一个自定义操作创建的文件“install.log”:

<Product>
    <CustomAction Id="Cleanup_logfile" Directory="INSTALLFOLDER"
    ExeCommand="cmd /C &quot;del install.log&quot;"
    Execute="deferred" Return="ignore" HideTarget="no" Impersonate="no" />

    <InstallExecuteSequence>
      <Custom Action="Cleanup_logfile" Before="RemoveFiles" >
        REMOVE="ALL"
      </Custom>
    </InstallExecuteSequence>
</Product>

据我了解,我不能使用“RemoveFile”,因为该文件是在安装后创建的,而不是组件组的一部分。

于 2013-07-07T15:37:38.990 回答
8

对于@Pavel的建议,这将是一个更完整的答案,对我来说它 100% 有效:

<Fragment Id="FolderUninstall">
    <?define RegDir="SYSTEM\ControlSet001\services\[Manufacturer]:[ProductName]"?>
    <?define RegValueName="InstallDir"?>
    <Property Id="INSTALLFOLDER">
        <RegistrySearch Root="HKLM" Key="$(var.RegDir)" Type="raw" 
                  Id="APPLICATIONFOLDER_REGSEARCH" Name="$(var.RegValueName)" />
    </Property>

    <DirectoryRef Id='INSTALLFOLDER'>
        <Component Id="UninstallFolder" Guid="*">
            <CreateFolder Directory="INSTALLFOLDER"/>
            <util:RemoveFolderEx Property="INSTALLFOLDER" On="uninstall"/>
            <RemoveFolder Id="INSTALLFOLDER" On="uninstall"/>
            <RegistryValue Root="HKLM" Key="$(var.RegDir)" Name="$(var.RegValueName)" 
                    Type="string" Value="[INSTALLFOLDER]" KeyPath="yes"/>
        </Component>
    </DirectoryRef>
</Fragment>

并且,在产品元素下:

<Feature Id="Uninstall">
    <ComponentRef Id="UninstallFolder" Primary="yes"/>
</Feature>

此方法使用要在卸载时删除的文件夹的所需路径设置注册表值。最后,从系统中删除 INSTALLFOLDER 和注册表文件夹。请注意,注册表的路径可以位于其他配置单元和其他位置。

于 2015-11-16T13:16:55.667 回答
6

不是 WIX 专家,但可能的(更简单的?)解决方案是运行作为WIX 内置扩展的一部分的安静执行自定义操作?

可以使用 /S 和 /Q 选项 运行rmdir MS DOS 命令。

<Binary Id="CommandPrompt" SourceFile="C:\Windows\System32\cmd.exe" />

完成这项工作的自定义操作很简单:

<CustomAction Id="DeleteFolder" BinaryKey="CommandPrompt" 
              ExeCommand='/c rmdir /S /Q "[CommonAppDataFolder]MyAppFolder\PurgeAppFolder"' 
              Execute="immediate" Return="check" />

然后你必须修改 InstallExecuteSequence 作为记录的许多地方。

更新: 这种方法有问题。最终改为制作自定义任务,但仍然认为这是一个可行的解决方案,但没有让细节起作用。

于 2010-02-02T12:47:31.973 回答