inno-setup – Inno Setup – 如何在安装过程中读取INF文件

我需要知道在安装过程中如何从INF文件[.inf]读取值.我希望安装程序检查我要更新的程序的版本,此程序版本不存储在注册表或任何其他文件中,仅存储在.inf文件中.然后必须从中获取版本.

我得到了你的答案,@ Tlama,我不能使用DLL来获得该软件的版本.
该程序仅将当前版本保存在INF文件中.

我想要做的是让安装程序检查我正在使用的软件的当前版本,并在标签文本中显示该版本.

inf信息是这样的:

NetVersion=1.1.1.1
PatchVersion=2.0.1
ProductName=SoftwareX

我只需要PatchVersion显示它所说的版本:####:

这是我想要解决的代码:

function GetInfsam: String;
var
  sVersion : String;
Begin
  sVersion := '';
  GetIniString('','PatchVersion','sVersion','{app}\Sam.inf');
  Result := sVersion;
end;

Procedure InitializeWizard7();
var
  L2Ver1 : Tlabel;
  L2Ver2 : Tlabel;
Begin
  L2Ver1:=  TLabel.Create(WizardForm);
  L2Ver1.Transparent:= True;
  L2Ver1.AutoSize:= False;
  L2Ver1.WordWrap:= True;
  L2Ver1.Font.name:= 'Agency FB';
  L2Ver1.Font.Size:= 12;
  L2Ver1.Font.Color:= clwhite;
  L2Ver1.Caption:= 'Version:';
  L2Ver1.Parent:= WizardForm.SelectdirPage;
  L2Ver1.Left := 5;
  L2Ver1.top := 260;
  L2Ver1.Width := 150;
  L2Ver1.Height := 40;

  L2Ver2:=  TLabel.Create(WizardForm);
  L2Ver2.Transparent:= True;
  L2Ver2.AutoSize:= False;
  L2Ver2.WordWrap:= True;
  L2Ver2.Font.name:= 'Agency FB';
  L2Ver2.Font.Size:= 12;
  L2Ver2.Font.Color:= clwhite;
  L2Ver2.Caption:= GetInfsam;
  L2Ver2.Parent:= WizardForm.SelectdirPage;
  L2Ver2.Left := L2Ver1.Width + L2Ver1.Left + 8;
  L2Ver2.top := 260;
  L2Ver2.Width := 100;
  L2Ver2.Height := 40;
End;

请,我需要帮助来修复我的代码.

解决方法

如何阅读INF文件?

INF文件只是specified syntax的INI文件.因此,要使用INF文件,您需要将它们视为普通的INI文件.假设您有一个这样的INF文件:

[Add.Code]
File.dll=File.dll

[File.dll]
File=http://www.code.com/file.dll
FileVersion=1,143

您可以通过这种方式使用GetIniString读取FileVersion密钥:

procedure InitializeWizard;
var
  Version: string;
begin
  Version := GetIniString('File.dll','FileVersion','','c:\File.inf');
  if Version <> '' then
    MsgBox('File version: ' + Version,mbInformation,MB_OK);
end;

更新:

1.格式错误的INF文件

根据您的更新,如果您的INF文件的内容如下所示:

NetVersion=1.1.1.1
PatchVersion=2.0.1
ProductName=SoftwareX

那么它不是一个格式良好的INF文件,而是一个用INF扩展名保存的名称值对文本文件.对于每个键值集,Real INF文件必须具有有效的[]部分,但文件中缺少此部分.

2.无法使用空的Section参数值调用GetIniString函数

不能使用空的Section参数值调用GetIniString函数,因为对于内部调用的函数GetPrivateProfileString,它意味着返回给定文件的所有节名,而不是指定键的值.因此,例如以下调用无效,因为第一个参数Section不能为空:

GetIniString('','KeyName','Default','c:\File.xxx');

3.如何使用名称值对文本文件?

您只需要像处理文本文件一样使用该文件.对于键值文本文件处理将是理想的使用TStringList类,或至少在Delphi中.在InnoSetup中,遗憾的是TStringList类没有键值内容操作所需的已发布属性,因此您需要创建自己的键值文本文件解析函数.这是获取给定键值的那个.因为键值分隔符应该是=符号.成功查找给定AFileName文件中的AKeyName键或失败时的默认ADefault值时,此函数返回键值:

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;

要从当前所选安装路径中预期的Sam.inf文件中读取PatchVersion键的值,您可以使用类似这样的内容.每当用户更改目录编辑框中的文本以及将要显示目录选择页面时,此脚本将更新标签值:

var
  // target version label must be declared globally
  L2Ver2: TLabel;

procedure DirEditChange(Sender: TObject);
var
  FilePath: string;
begin
  // assign the expected INF file path
  FilePath := AddBackslash(WizardForm.DirEdit.Text) + 'Sam.inf';
  // read the PatchVersion key value,return N/A if not found
  L2Ver2.Caption := GetKeyValue('PatchVersion',FilePath,'N/A');
end;

procedure InitializeWizard;
begin
  // create the target label as before
  L2Ver2 := TLabel.Create(WizardForm);
  ...
  // bind the DirEditChange method to the directory edit's OnChange event
  WizardForm.DirEdit.OnChange := @DirEditChange;  
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  // if the page has been turned to the select directory page,update the
  // label caption by firing the assigned OnChange event method manually
  if (CurPageID = wpSelectDir) then
    DirEditChange(nil);
end;

相关文章

 从网上看到《Delphi API HOOK完全说明》这篇文章,基本上都...
  从网上看到《Delphi API HOOK完全说明》这篇文章,基本上...
ffmpeg 是一套强大的开源的多媒体库 一般都是用 c/c+&#x...
32位CPU所含有的寄存器有:4个数据寄存器(EAX、EBX、ECX和ED...
1 mov dst, src dst是目的操作数,src是源操作数,指令实现的...