在Inno Setup中比较版本字符串 [英] Compare version strings in Inno Setup

查看:367
本文介绍了在Inno Setup中比较版本字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取INF文件的值,现在需要将其与安装程序版本进行比较,但是在编译时出现错误:

I'm reading the value of an INF file, now I need to compare it with the installer version, but when I compile I get an error:

未知标识符:CompareVersion

Unknown identifier: CompareVersion

怎么了?

[Code]

function GetKeyValue(const AKeyName, AFileName, ADefault: string): string;
var  
  I: Integer;
  KeyPos: Integer;
  KeyFull: string;
  FileLines: TArrayOfString;
begin
  Result := ADefault;
  if LoadStringsFromFile(AFileName, FileLines) then
  begin
    KeyFull := AKeyName + '=';
    for I := 0 to GetArrayLength(FileLines) - 1 do
    begin
      FileLines[I] := TrimLeft(FileLines[I]);
      KeyPos := Pos(KeyFull, FileLines[I]);
      if KeyPos > 0 then
      begin
        Result := Copy(FileLines[I], KeyPos + Length(AKeyName) + 1, MaxInt);
        Break;
      end;
    end;
  end;
end;

var
  L2Ver2: TLabel;

procedure DirEditChange(Sender: TObject);
var
  FilePath: string;
begin
  FilePath := AddBackslash(WizardForm.DirEdit.Text) + 'left4dead2\steam.inf';
  L2Ver2.Caption := GetKeyValue('PatchVersion', FilePath, 'N/A');
  if (CompareVersion( L2Ver2.Caption, '{#MyAppOldVersion}') = 0) then
    begin
      ...
    end 
end;

我在这一行有错误:

if (CompareVersion( L2Ver2.Caption, '{#MyAppOldVersion}') = 0) then

{#MyAppOldVersion}已经定义.

这篇文章与我以前的问题有关:在安装过程中读取INF文件.

This post is related to my previous question: Read an INF file during the Setup.

如果有人可以提供帮助,我将不胜感激.

If anyone can help, I would appreciate.

推荐答案

Inno Setup中没有CompareVersion功能.

There's no CompareVersion function in Inno Setup.

您需要定义自己的内容.

You need to define your own.

类似的东西:

function CompareVersion(V1, V2: string): Integer;
var
  P, N1, N2: Integer;
begin
  Result := 0;
  while (Result = 0) and ((V1 <> '') or (V2 <> '')) do
  begin
    P := Pos('.', V1);
    if P > 0 then
    begin
      N1 := StrToInt(Copy(V1, 1, P - 1));
      Delete(V1, 1, P);
    end
      else
    if V1 <> '' then
    begin
      N1 := StrToInt(V1);
      V1 := '';
    end
      else
    begin
      N1 := 0;
    end;

    P := Pos('.', V2);
    if P > 0 then
    begin
      N2 := StrToInt(Copy(V2, 1, P - 1));
      Delete(V2, 1, P);
    end
      else
    if V2 <> '' then
    begin
      N2 := StrToInt(V2);
      V2 := '';
    end
      else
    begin
      N2 := 0;
    end;

    if N1 < N2 then Result := -1
      else
    if N1 > N2 then Result := 1;
  end;
end;

  • 如果版本相同,则返回0.
  • 如果V1早于V2,则
  • 返回-1.
  • 如果V1V2更新,则返回1.
  • 1.12被认为比1.1更新.
  • 1.1被认为与1.1.0相同.
  • 当版本在语法上无效(仅允许数字和点)时,引发异常.
    • Returns 0, if the versions are equal.
    • Returns -1, if the V1 is older than the V2.
    • Returns 1, if the V1 is newer than the V2.
    • 1.12 is considered newer than 1.1.
    • 1.1 is considered the same as 1.1.0.
    • Throws an exception, when a version is syntactically invalid (only digits and dots are allowed).
    • 单元测试:

      procedure CompareVersionTest(V1, V2: string; Expected: Integer);
      var
        R: Integer;
        M: string;
      begin
        M := Format('Comparing [%s] vs. [%s] - expecting %d - ', [V1, V2, Expected]);
        R := CompareVersion(V1, V2);
        if R <> Expected then
        begin
          RaiseException(M + Format('Failed - Got %d', [R]));
        end
          else
        begin
          Log(M + 'Passed');
        end;
      end;
      
      procedure CompareVersionTests;
      begin
        CompareVersionTest('1', '1', 0);
        CompareVersionTest('1.2', '1.2', 0);
        CompareVersionTest('1.2.3', '1.2.3', 0);
        CompareVersionTest('1', '2', -1);
        CompareVersionTest('1', '3', -1);
        CompareVersionTest('2', '3', -1);
        CompareVersionTest('2', '1', 1);
        CompareVersionTest('3', '1', 1);
        CompareVersionTest('3', '2', 1);
        CompareVersionTest('1', '12', -1);
        CompareVersionTest('12', '2', 1);
        CompareVersionTest('12', '12', 0);
        CompareVersionTest('1.2', '1.3', -1);
        CompareVersionTest('1.3', '1.2', 1);
        CompareVersionTest('1.2', '11.2', -1);
        CompareVersionTest('11.2', '1.2', 1);
        CompareVersionTest('1.1', '1.12', -1);
        CompareVersionTest('1.11', '1.12', -1);
        CompareVersionTest('1.12', '1.1', 1);
        CompareVersionTest('1.12', '1.11', 1);
        CompareVersionTest('1', '1.0', 0);
        CompareVersionTest('1.0', '1', 0);
        CompareVersionTest('1', '1.1', -1);
        CompareVersionTest('1.1', '1', 1);
        CompareVersionTest('1.12.123', '1.12.124', -1);
        CompareVersionTest('1.12.124', '1.12.123', 1);
        CompareVersionTest('1.12.123', '1.13.1', -1);
        CompareVersionTest('1.13.1', '1.12.123', 1);
        CompareVersionTest('1.12.123', '1.13', -1);
        CompareVersionTest('1.13', '1.12.123', 1);
        CompareVersionTest('1.12', '1.13.1', -1);
        CompareVersionTest('1.13.1', '1.12', 1);
      end;
      

      如果要运行单元测试,只需在CompareVersionTests > InitializeSetup .

      If you want to run the unit tests, just call CompareVersionTests in InitializeSetup.

      这篇关于在Inno Setup中比较版本字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆