0
我需要打開INI文件並讀取特定值並檢查是否有不同的更改。Inno Setup修改文本文件並更改特定行
但是情況是我的INI文件沒有
例如,該文件包含以下只有2條線的任何部分或鍵值。我需要的是讀第二行(應該是16001)。如果不匹配,請更換那個。
[email protected]
16000
請提出任何意見,這對我會很有幫助!
預先感謝您。
我需要打開INI文件並讀取特定值並檢查是否有不同的更改。Inno Setup修改文本文件並更改特定行
但是情況是我的INI文件沒有
例如,該文件包含以下只有2條線的任何部分或鍵值。我需要的是讀第二行(應該是16001)。如果不匹配,請更換那個。
[email protected]
16000
請提出任何意見,這對我會很有幫助!
預先感謝您。
您的文件不是INI文件。它不僅沒有部分,甚至沒有鑰匙。
您必須將文件編輯爲純文本文件。您不能使用INI文件功能。
這段代碼就可以了:
function GetLastError(): LongInt; external '[email protected] stdcall';
function SetLineInFile(FileName: string; Index: Integer; Line: string): Boolean;
var
Lines: TArrayOfString;
Count: Integer;
begin
if not LoadStringsFromFile(FileName, Lines) then
begin
Log(Format('Error reading file "%s". %s', [FileName, SysErrorMessage(GetLastError)]));
Result := False;
end
else
begin
Count := GetArrayLength(Lines);
if Index >= GetArrayLength(Lines) then
begin
Log(Format('There''s no line %d in file "%s". There are %d lines only.', [
Index, FileName, Count]));
Result := False;
end
else
if Lines[Index] = Line then
begin
Log(Format('Line %d in file "%s" is already "%s". Not changing.', [
Index, FileName, Line]));
Result := True;
end
else
begin
Log(Format('Updating line %d in file "%s" from "%s" to "%s".', [
Index, FileName, Lines[Index], Line]));
Lines[Index] := Line;
if not SaveStringsToFile(FileName, Lines, False) then
begin
Log(Format('Error writting file "%s". %s', [
FileName, SysErrorMessage(GetLastError)]));
Result := False;
end
else
begin
Log(Format('File "%s" saved.', [FileName]));
Result := True;
end;
end;
end;
end;
這樣使用它:
SetLineInFile(ExpandConstant('{app}\Myini.ini'), 1, '16001');
(索引從零開始)
謝謝! :)按預期工作! – zooha
不客氣。雖然在StackOverflow我們[感謝接受答案](http://stackoverflow.com/help/someone-answers)。 –