Inno Setup-如何在安装过程中读取INF文件 [英] Inno Setup - How to read an INF file during the Setup

查看:103
本文介绍了Inno Setup-如何在安装过程中读取INF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i need to know how to read a value from INF file [.inf], during the setup. I want the installer to check the version of the program that i am going to update, This program version is not stored in the registry or any other file, is only in the .inf file. Then is a must to get the version from it.

我得到了您的答案,@ Tlama,并且我无法使用DLL来获取软件的版本. 该程序仅将当前版本保存在INF文件中.

I got your answers, @Tlama and i cannot use a DLL to get version of the software. This program only save the current version in the INF file.

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

What i want to do, is to make the installer to check the current versión of the software that i am working with, and display that version in a label text.

inf信息是这样的:

The inf information is this:

NetVersion=1.1.1.1
PatchVersion=2.0.1
ProductName=SoftwareX

我只需要PatchVersion在显示版本的位置后显示:####:

I just need the PatchVersion to display after where it says version: #### :

这是我要修复的代码:

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;

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

Please, i need help to fix my code.

推荐答案

如何读取INF文件?

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

How to read INF file ?

INF files are just sort of INI files with the specified syntax. So to work with INF files you need to treat them as ordinary INI files. Assuming you have a INF file like this:

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

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

您可以阅读 FileVersion 通过使用 GetIniString 这样的方式输入密钥:

You can read the FileVersion key by using GetIniString this way:

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文件的内容如下所示:

According to your update, if the content of your INF file looks like this:

NetVersion=1.1.1.1
PatchVersion=2.0.1
ProductName=SoftwareX

这不是一个格式正确的INF文件,而是一个以INF扩展名保存的名称/值对文本文件.对于每个键值集,实际的INF文件必须具有有效的[]部分,但是该部分在您的文件中丢失.

then it's not a well formed INF file, but a name value pair text file saved with INF extension. Real INF files must have a valid [] section for each key value set, but this section is missing in your file.

2.不能使用空的Section参数值来调用GetIniString函数

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

You must not call the GetIniString function with empty Section parameter value, because for the internally called GetPrivateProfileString function it means to return all section names for a given file, not value of a specified key. So for instance the following call is invalid, because the first parameter Section cannot be empty:

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

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

您只需要像处理文本文件一样使用该文件.对于键值文本文件,使用TStringList类或至少在Delphi中进行处理将是理想的选择.不幸的是,在InnoSetup中,TStringList类没有键值内容操作所需的已发布属性,因此您需要创建自己的键值文本文件解析功能.这是为给定键获取价值的一种.由于键值定界符应该是=符号.当成功在给定的AFileName文件中找到AKeyName键时,此函数返回键值;失败时返回默认的ADefault值:

You'll just need to work with that file as with a text file. For a key value text file handling would be ideal to use the TStringList class, or at least in Delphi. In InnoSetup unfortunately the TStringList class doesn't have published properties needed for a key value content manipulation, so you'll need to make your own key value text file parsing function. Here's the one for getting value for a given key. As the key value delimiter is supposed to be the = sign. This function returns a key value when succeed to find a AKeyName key in a given AFileName file or default ADefault value when fails:

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项的值,可以使用类似这样的内容.每当您的用户在目录编辑框中更改文本以及将要显示目录选择页面时,此脚本都会更新标签值:

To read a value of the PatchVersion key from the Sam.inf file expected in the currently selected install path you can use something like this. This script will update the label value whenever your user change the text in the directory edit box and when the directory selection page is going to be displayed:

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;

这篇关于Inno Setup-如何在安装过程中读取INF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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