2016-03-11 274 views
0

我需要打开INI文件并读取特定值并检查是否有不同的更改。Inno Setup修改文本文件并更改特定行

但是情况是我的INI文件没有

例如,该文件包含以下只有2条线的任何部分或键值。我需要的是读第二行(应该是16001)。如果不匹配,请更换那个。

[email protected] 
16000 

请提出任何意见,这对我会很有帮助!

预先感谢您。

回答

3

您的文件不是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'); 

(索引从零开始)

+0

谢谢! :)按预期工作! – zooha

+0

不客气。虽然在StackOverflow我们[感谢接受答案](http://stackoverflow.com/help/someone-answers)。 –

相关问题