9

如何告诉 InnoSetup 不卸载已被用户更改的(文本)文件(== 与 InnoSetup 安装的文件不同)?

或者可能更困难:在现有版本上安装新版本时,InnoSetup 应该询问用户是否覆盖更改的文件,但在纯卸载时,它应该在不询问的情况下将其卸载。

4

2 回答 2

6

我最近遇到了类似的问题。这是我检测文本文件(配置文件)是否已从上次安装运行期间安装的文件更改的解决方案:

使用 ISPP(Inno Setup Pre-Processor)在编译时创建文本文件列表及其哈希:

[Files]
; ...
#define FindHandle
#define FindResult
#define Mask "Profiles\*.ini"
#sub ProcessFoundFile
   #define FileName "Profiles\" + FindGetFileName(FindHandle)
   #define FileMd5 GetMd5OfFile(FileName)
   Source: {#FileName}; DestDir: {app}\Profiles; Components: profiles; \
      Check: ProfileCheck('{#FileMd5}'); AfterInstall: ProfileAfterInstall('{#FileMd5}');
#endsub
#for {FindHandle = FindResult = FindFirst(Mask, 0); FindResult; FindResult = FindNext(FindHandle)} ProcessFoundFile

在“代码”部分的顶部,我定义了一些有用的东西:

[Code]
var
   PreviousDataCache : tStringList;

function InitializeSetup() : boolean;
begin
   // Initialize global variable
   PreviousDataCache := tStringList.Create();
   result := TRUE;
end;

function BoolToStr( Value : boolean ) : string;
begin
   if ( not Value ) then
      result := 'false'
   else
      result := 'true';
end;

在“检查”事件处理程序中,我比较了以前安装和当前文件的哈希值:

function ProfileCheck( FileMd5 : string ) : boolean;
var
   TargetFileName, TargetFileMd5, PreviousFileMd5 : string;
   r : integer;
begin
   result := FALSE;
   TargetFileName := ExpandConstant(CurrentFileName());
   Log('Running check procedure for file: ' + TargetFileName);

   if not FileExists(TargetFileName) then
   begin
      Log('Check result: Target file does not exist yet.');
      result := TRUE;
      exit;
   end;

   try
      TargetFileMd5 := GetMd5OfFile(TargetFileName);
   except
      TargetFileMd5 := '(error)';
   end;
   if ( CompareText(TargetFileMd5, FileMd5) = 0 ) then
   begin
      Log('Check result: Target matches file from setup.');
      result := TRUE;
      exit;
   end;

   PreviousFileMd5 := GetPreviousData(ExtractFileName(TargetFileName), '');
   if ( PreviousFileMd5 = '' ) then
   begin
      r := MsgBox(TargetFileName + #10#10 + 
         'The existing file is different from the one Setup is trying to install. ' + 
         'It is recommended that you keep the existing file.' + #10#10 +
         'Do you want to keep the existing file?', mbConfirmation, MB_YESNO);
      result := (r = idNo);
      Log('Check result: ' + BoolToStr(result));
   end
   else if ( CompareText(PreviousFileMd5, TargetFileMd5) <> 0 ) then
   begin
      r := MsgBox(TargetFileName + #10#10 + 
         'The existing file has been modified since the last run of Setup. ' +
         'It is recommended that you keep the existing file.' + #10#10 +
         'Do you want to keep the existing file?', mbConfirmation, MB_YESNO);
      result := (r = idNo);
      Log('Check result: ' + BoolToStr(result));
   end
   else
   begin
      Log('Check result: Existing target has no local modifications.');
      result := TRUE;
   end;
end;

在“AfterInstall”事件处理程序中,我将文件哈希标记为稍后存储在注册表中。因为在我的测试中,即使文件移动失败(目标文件是只读的)也会触发事件,所以我再次比较哈希以确定文件移动是否成功:

procedure ProfileAfterInstall( FileMd5 : string );
var
   TargetFileName, TargetFileMd5 : string;
begin
   TargetFileName := ExpandConstant(CurrentFileName());
   try
      TargetFileMd5 := GetMd5OfFile(TargetFileName);
   except
      TargetFileMd5 := '(error)';
   end;
   if ( CompareText(TargetFileMd5, FileMd5) = 0 ) then
   begin
      Log('Storing hash of installed file: ' + TargetFileName);
      PreviousDataCache.Add(ExtractFileName(TargetFileName) + '=' + FileMd5);
   end;
end;

procedure RegisterPreviousData( PreviousDataKey : integer );
var
   Name, Value : string;
   i, n : integer;
begin
   for i := 0 to PreviousDataCache.Count-1 do
   begin
      Value := PreviousDataCache.Strings[i];
      n := Pos('=', Value);
      if ( n > 0 ) then
      begin
         Name := Copy(Value, 1, n-1);
         Value := Copy(Value, n+1, MaxInt);
         SetPreviousData(PreviousDataKey, Name, Value);
      end;
   end;
end;
于 2012-05-29T08:02:59.947 回答
1

Inno 本身无法执行此检查。

要在安装期间不替换更改的文件,您需要使用 custom[Code]进行校验和并与预先计算或从先前安装保存的已知良好值进行比较。

为避免在卸载期间删除它们,您需要禁用 Inno 自己对该文件的卸载,并在删除它们之前检查相同的校验和,再次在[Code].

请注意,最好将用户可以编辑的任何文件保留在设置之外,以便更好地处理这种情况并正确遵守应用程序指南。

于 2012-05-22T15:04:27.390 回答