我需要安装程序设置在安装过程中检查 txt 文件的第一行,并将其与我想要的任何数字进行比较。
这是txt文件:

这是我正在尝试编辑的代码:
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
// 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';
// I WANT TO READ THE FIRST LINE OF THE TXT FILE AND return N/A if not found
L2Ver2.Caption := GetKeyValue('', 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)
我从这篇文章中获得了代码:Inno Setup - How to read an INF file during the Setup
我不确定如何编辑function GetKeyValue代码的其他部分L2Ver2.Caption := GetKeyValue('', FilePath, 'N/A');
除了该LoadStringsFromFile函数之外,您无法从该函数中使用太多内容。我编写的下一个函数加载由参数给出的文本文件FileName,并尝试将行从基于 0 的索引复制Index到输出参数Line。如果给定文件的加载成功并且该文件有足够的行来满足请求Index,则返回 True,否则返回 False。
function TryGetFileLine(const FileName: string; Index: Integer; out Line: string): Boolean;
var
FileLines: TArrayOfString;
begin
// the function succeed when the file can be loaded and the count of lines is
// greater than the requested line index (it is 0 based index, hence the line
// count must be greater)
Result := LoadStringsFromFile(FileName, FileLines) and (GetArrayLength(FileLines) > Index);
// if the above succeeded, return the file line of the requested index to the
// output parameter
if Result then
Line := FileLines[Index];
end;
Run Code Online (Sandbox Code Playgroud)
对于较短的代码,我选择基于 0 的索引,因此如果您想读取文件的第一行,您将请求索引 0、第二行索引 1 等等。要获得第一行,它将是:
var
S: string;
begin
// if the file could be opened and has at least one line, then the following
// call succeed and the variable S will contain the first line of the file
if TryGetFileLine('C:\File.txt', 0, S) then
MsgBox('The first line of the given file is: ' + S, mbInformation, MB_OK);
end;
Run Code Online (Sandbox Code Playgroud)
此时Inno Setup中至少有两种方法可以将字符串转换为32位整数。函数StrToInt和StrToIntDef. 第一个尝试将传递的字符串转换为整数,如果失败,则返回 -1。第二个执行相同的操作,但如果转换失败,则返回参数指定的值def。
不幸的是,上述函数都无法可靠地判断给定字符串是否已转换而不丢失整数范围内的一个值。考虑以下代码:
var
Value1: Integer;
Value2: Integer;
begin
Value1 := StrToInt('-1');
Value2 := StrToInt('Non integer');
if Value1 = Value2 then
begin
MsgBox('Err, the result is the same for string of value -1 and for a non ' +
'integer string.', mbInformation, MB_OK);
end;
end;
Run Code Online (Sandbox Code Playgroud)
上面的代码演示了,如果您使用该StrToInt函数,您将无法确定字符串(在您的情况下是从文件读取的行)是否包含该值-1或非整数值。类似的情况也适用于函数def的参数StrToIntDef。
但是,如果您显式检查字符串是否包含函数在转换失败时返回的值,则可以解决此问题。S如果字符串包含有效的整数值,则以下函数返回 True ,否则返回 False。如果转换成功,则将转换后的值返回到输出Value参数:
function TryStrToInt(const S: string; out Value: Integer): Boolean;
var
I: Integer;
begin
I := StrToIntDef(S, -1);
Result := (I <> -1) or (S = '-1');
if Result then
Value := I;
end;
Run Code Online (Sandbox Code Playgroud)
该函数的用法如下所示:
var
I: Integer;
begin
if TryStrToInt('12345', I) then
begin
MsgBox('The passed string was converted to integer. Its value is: ' +
IntToStr(I), mbInformation, MB_OK);
end;
end;
Run Code Online (Sandbox Code Playgroud)
您没有提到文本文件中的版本值是什么,所以我假设它们是 32 位整数,并且可能具有该范围内的任何值(尽管我相信实际上您只会使用正值,其中内置的字符串到整数转换函数可能就足够了)。
尽管如此,在代码库中拥有更安全的字符串到整数转换函数还是可以的。因此,让我们将上述函数组合在一起,尝试读取文本文件的第一行并将其转换为整数:
var
S: string;
I: Integer;
begin
// if the first line of the file was successfully read and could have been
// converted to integer, then...
if TryGetFileLine('C:\File.txt', 0, S) and TryStrToInt(S, I) then
begin
MsgBox('The first line of the given file was successfully read and could ' +
'have been converted to integer. Its value is: ' + IntToStr(I),
mbInformation, MB_OK);
// here the variable I contains the value that you can compare in a way
// of your choice
end;
end;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5202 次 |
| 最近记录: |