wix uninstaller(exe) 如何删除自身

How wix uninstaller(exe) can remove itself

我有我的自定义安装程序和卸载程序,它们将 MSI 和其他资源安装到 PC。卸载过程在以下几行内进行:

<DirectoryRef Id="TARGETDIR">
    <Component Id="AddRemovePrograms" Guid="*" KeyPath="yes">
      <RegistryValue Id="ARPEntry1" Type="string" Action="write" Root="HKLM" Key="Software\Microsoft\Windows\CurrentVersion\Uninstall$(var.ProductCode)" Name="DisplayName" Value="$(var.ProductName)"/>
      <RegistryValue Id="ARPEntry2" Type="string" Action="write" Root="HKLM" Key="Software\Microsoft\Windows\CurrentVersion\Uninstall$(var.ProductCode)" Name="DisplayVersion" Value="$(var.ProductVersion)"/>
      <RegistryValue Id="ARPEntry3" Type="string" Action="write" Root="HKLM" Key="Software\Microsoft\Windows\CurrentVersion\Uninstall$(var.ProductCode)" Name="Publisher" Value="$(var.Manufacturer)"/>
      <RegistryValue Id="ARPEntry4" Type="integer" Action="write" Root="HKLM" Key="Software\Microsoft\Windows\CurrentVersion\Uninstall$(var.ProductCode)" Name="NoModify" Value="1"/>
      <RegistryValue Id="ARPEntry5" Type="string" Action="write" Root="HKLM" Key="Software\Microsoft\Windows\CurrentVersion\Uninstall$(var.ProductCode)" Name="UninstallString" Value="[CommonAppDataFolder]\[Manufacturer]\[ProductName]\Uninstaller.exe"/>
      <RegistryValue Id="ARPEntry6" Type="string" Action="write" Root="HKLM" Key="Software\Microsoft\Windows\CurrentVersion\Uninstall$(var.ProductCode)" Name="InternalVersion" Value="$(var.ProductVersion)"/>
    </Component>
    <Directory Id="CommonAppDataFolder">
      <Directory Id="UninstallCompanyDir" Name="$(var.Manufacturer)">
        <Directory Id="UninstallProductDir" Name="$(var.ProductName)">
          <Component Id="UninstallerExe" Guid="*">
            <File Id="UninstallerExeFile" Name="Uninstaller.exe" Source="..\Uninstaller.exe" Vital="yes" KeyPath="yes">
            </File>
          </Component>
        </Directory>
      </Directory>
    </Directory>
  </DirectoryRef>  

在 Uninstaller.exe 中,我将自身复制到 TEMP 文件夹并从那里 运行,但问题是我的卸载程序留在了那里(在 TEMP 中)。

问题: 如何使用 wix 脚本删除我的可执行文件(来自 TEMP 或原始文件)?

您可以使用批处理来完成!

类似

cmd.exe /C TIMEOUT 10 && del "{your uninstaller path}"

你 运行 它在卸载程序关闭事件中。这将产生一个新的 cmd 进程并在 10 秒后执行删除命令。

创建以下批处理文件。这将 运行 卸载程序,当卸载程序完成后,它将同时删除卸载程序和批处理文件。

START /WAIT YourUninstaller.exe
Del YourUninstaller.exe
Del ThisBatchFile.bat

对于这种情况,有一个已记录的 "how to",它不需要您拥有可执行文件,使用 msiexec.exe 而不是您自己的可执行文件:

How To: Create an Uninstall Shortcut

你没有说你的 exe 是否除了调用卸载之外做任何事情,但我认为复制到一个临时文件夹并将可执行文件留在那里是完全可以接受的(它不需要是一个 exe 因为你可以将其作为 .tmp 文件调用 CreateProcess)。有一些标准工具可以清除临时文件夹(磁盘清理、服务器脚本),所以不用担心。

一般来说,从 Windows 10 开始,您不需要在开始菜单上进行卸载。无论如何,右键单击已安装的应用程序都会出现卸载,它甚至可能会抑制您的应用程序。