有时需要删除在安装时最初未写入用户磁盘的文件。其中一种情况是您的应用程序在启动时会自行更新。可以以这种方式将不属于卸载程序的新文件添加到磁盘中。
对于这种情况,我建议您创建一个“补丁清单”文件,该文件会记录 {app} 目录中应包含哪些文件。在下面找到从 {app} 目录中名为“patch_manifest.txt”的文件中读取的代码示例
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
i: Integer;
arrayLen: Longint;
item: String;
itemsToDelete: Array of String;
begin
case CurUninstallStep of
usUninstall:
begin
LoadStringsFromFile(ExpandConstant('{app}') + '\patch_manifest.txt', itemsToDelete);
arrayLen := GetArrayLength(itemsToDelete);
for i := 0 to arrayLen-1 do
begin
item := ExpandConstant('{app}') + '\' + itemsToDelete[i];
if FileExists(item) then
DeleteFile(item);
if DirExists(item) then
RemoveDir(item);
end;
end;
end;
end;
和 patch_manifest.txt 的样本
data/something_here.dat
data/moredatahere.dat
data/
Launcher.exe
patch_manifest.txt
注意:patch_manifest 中的行顺序很重要。目录中的所有文件应首先列出,然后是目录 - 非空目录不能删除。
您的应用程序应该附带一个 patch_manifest,并且 patch_manifest 应该随每个补丁更新。将此作为构建过程的一部分,这样您就不会忘记更新它!
即使提示用户,也不要使用通配符 ( . ) 删除,这一点非常重要。卸载程序具有提升的权限,这可能会破坏用户的计算机。以用户不小心将您的应用程序安装到 C:\Windows\ 或 C:\Program Files 为例。
另一个好主意是通过在删除之前执行 MD5 检查来验证该文件实际上是“您的文件”。在这种情况下,您的 patch_manifest.txt 不仅包括文件的相对路径,还包括 MD5 校验和。