我需要知道在安装过程中如何从INF文件[.inf]读取值.我希望安装程序检查我要更新的程序的版本,此程序版本不存储在注册表或任何其他文件中,仅存储在.inf文件中.然后必须从中获取版本.
我得到了你的答案,@ Tlama,我不能使用DLL来获得该软件的版本.该程序仅将当前版本保存在INF文件中.
我想要做的是让安装程序检查我正在使用的软件的当前版本,并在标签文本中显示该版本.
inf信息是这样的:
NetVersion=1.1.1.1
PatchVersion=2.0.1
ProductName=SoftwareX
Run Code Online (Sandbox Code Playgroud)
我只需要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;
Run Code Online (Sandbox Code Playgroud)
请,我需要帮助来修复我的代码.
TLa*_*ama 10
INF文件只是一种带有的INI文件specified syntax.因此,要使用INF文件,您需要将它们视为普通的INI文件.假设您有一个这样的INF文件:
[Add.Code]
File.dll=File.dll
[File.dll]
File=http://www.code.com/file.dll
FileVersion=1,0,0,143
Run Code Online (Sandbox Code Playgroud)
您可以FileVersion使用GetIniString以下方式读取密钥:
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;
Run Code Online (Sandbox Code Playgroud)
1.格式错误的INF文件
根据您的更新,如果您的INF文件的内容如下所示:
NetVersion=1.1.1.1
PatchVersion=2.0.1
ProductName=SoftwareX
Run Code Online (Sandbox Code Playgroud)
那么它不是一个格式良好的INF文件,而是一个用INF扩展名保存的名称值对文本文件.Real INF文件必须具有[]每个键值集的有效部分,但文件中缺少此部分.
2.无法使用空的Section参数值调用GetIniString函数
您不能GetIniString使用空Section参数值调用该函数,因为对于内部调用的GetPrivateProfileString函数,它意味着返回给定文件的所有节名称,而不是指定键的值.因此,例如以下调用无效,因为第一个参数Section不能为空:
GetIniString('', 'KeyName', 'Default', 'c:\File.xxx');
Run Code Online (Sandbox Code Playgroud)
3.如何使用名称值对文本文件?
您只需要像处理文本文件一样使用该文件.对于键值文本文件处理将是理想的使用TStringList类,或至少在Delphi中.不幸的是,在InnoSetup中,TStringList类没有键值内容操作所需的已发布属性,因此您需要创建自己的键值文本文件解析功能.这是获取给定键值的那个.因为键值定界符应该是=符号.成功查找AKeyName给定AFileName文件中的键或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;
Run Code Online (Sandbox Code Playgroud)
要从当前所选安装路径中预期PatchVersion的Sam.inf文件中读取密钥的值,您可以使用类似这样的内容.每当用户更改目录编辑框中的文本以及将要显示目录选择页面时,此脚本将更新标签值:
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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4764 次 |
| 最近记录: |