如何读取INF文件?
INF 文件只是 INI 文件的一种specified syntax http://msdn.microsoft.com/en-us/library/aa741215%28v=vs.85%29.aspx。因此,要使用 INF 文件,您需要将它们视为普通 INI 文件。假设你有一个像这样的 INF 文件:
[Add.Code]
File.dll=File.dll
[File.dll]
File=http://www.code.com/file.dll
FileVersion=1,0,0,143
您可以阅读FileVersion http://msdn.microsoft.com/en-us/library/aa741215%28v=vs.85%29.aspx#keys密钥通过使用GetIniString http://jrsoftware.org/ishelp/topic_isxfunc_getinistring.htm这边走:
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;
Update:
1.INF文件格式错误
根据您的更新,如果您的 INF 文件的内容如下所示:
NetVersion=1.1.1.1
PatchVersion=2.0.1
ProductName=SoftwareX
那么它不是一个格式良好的 INF 文件,而是一个以 INF 扩展名保存的名称值对文本文件。真正的INF文件必须有一个有效的[]
每个键值集的部分,但您的文件中缺少此部分。
2. 无法使用空的Section参数值调用GetIniString函数
您不得致电GetIniString http://jrsoftware.org/ishelp/topic_isxfunc_getinistring.htm函数为空Section
参数值,因为对于内部调用GetPrivateProfileString http://msdn.microsoft.com/en-us/library/windows/desktop/ms724353%28v=vs.85%29.aspx函数意味着返回给定文件的所有节名称,而不是指定键的值。例如下面的调用是无效的,因为第一个参数Section
不能为空:
GetIniString('', 'KeyName', 'Default', 'c:\File.xxx');
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;
读取一个值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;