将如下代码追加到NSIS脚本的尾部即可。
Var UNINSTALL_PROG
Function .onInit
ClearErrors
ReadRegStr $UNINSTALL_PROG ${PRODUCT_UNINST_ROOT_KEY} ${PRODUCT_UNINST_KEY} "UninstallString"
IfErrors done
MessageBox MB_YESNOCANCEL|MB_ICONQUESTION
"检测到本机已经安装了 ${PRODUCT_NAME}。
$
$
是否先卸载已安装的版本?"
/SD IDYES
IDYES uninstall
IDNO done
Abort
uninstall:
CreateDirectory C:WINDOWS emp
CopyFiles $UNINSTALL_PROG C:WINDOWS empuninst.exe
ExecWait '"C:WINDOWS empuninst.exe" /S _?=C:WINDOWS emp' $0
DetailPrint "uninst.exe returned $0"
Delete "C:WINDOWS empuninst.exe"
done:
FunctionEnd