2017-08-21 64 views
1

我正在为我的应用程序创建Inno Setup安装程序/更新程序。现在我需要找到一种方法来检查新版本是否可用,如果可用,它应该自动安装在已安装的版本上。解析Inno Setup中用于检查版本号的键值文本文件

特殊情况是版本号与其他数据在一个文件中。 是Inno Setup的需要阅读看起来像文件:

#Eclipse Product File 
#Fri Aug 18 08:20:35 CEST 2017 
version=0.21.0 
name=appName 
id=appId 

我已经找到了一种方法来更新使用脚本,只有阅读与它的版本号的文本文件的应用程序。 Inno setup: check for new updates

但在我的情况下,它包含更多的数据,安装程序不需要。有人可以帮助我构建一个可以从文件中解析版本号的脚本吗?

,我已经有看起来像代码:

function GetInstallDir(const FileName, Section: string): string; 
var 
    S: string; 
    DirLine: Integer; 
    LineCount: Integer; 
    SectionLine: Integer;  
    Lines: TArrayOfString; 
begin 
    Result := ''; 
Log('start'); 
    if LoadStringsFromFile(FileName, Lines) then 
    begin 
Log('Loaded file'); 
    LineCount := GetArrayLength(Lines); 
    for SectionLine := 0 to LineCount - 1 do 

Log('File line ' + lines[SectionLine]); 


    if (pos('version=', Lines[SectionLine]) <> 0) then 
       begin 
        Log('version found'); 
        S := RemoveQuotes(Trim(Lines[SectionLine])); 
        StringChangeEx(S, '\\', '\', True); 
        Result := S; 
        Exit; 
       end; 
    end; 
end; 

但运行时,脚本检查检查,如果版本字符串就行不起作用。

+0

Stack Overflow是不是一个自由的编码服务,虽然有时看起来像[InnoSetup是 –

+0

可能重复:如何通过阅读声明一个变量从一个文件](https://stackoverflow.com/questions/14530504/innosetup-how-to-declare-a-variable-by-reading-from-a-file) – Miral

+0

它是一个INI文件?是否有一个部分开始?或者这是真的整个文件,因为它是? –

回答

1

你的代码几乎是正确的。您只在代码周围缺少beginend,您想在for循环中重复该操作。所以只有Log行重复;并且if针对超出范围的LineCount索引执行。

如果格式化代码更好它变得很明显,:

function GetInstallDir(const FileName, Section: string): string; 
var 
    S: string; 
    DirLine: Integer; 
    LineCount: Integer; 
    SectionLine: Integer;  
    Lines: TArrayOfString; 
begin 
    Result := ''; 
    Log('start'); 
    if LoadStringsFromFile(FileName, Lines) then 
    begin 
    Log('Loaded file'); 
    LineCount := GetArrayLength(Lines); 
    for SectionLine := 0 to LineCount - 1 do 
    begin { <--- Missing } 
     Log('File line ' + lines[SectionLine]); 

     if (pos('version=', Lines[SectionLine]) <> 0) then 
     begin 
     Log('version found'); 
     S := RemoveQuotes(Trim(Lines[SectionLine])); 
     StringChangeEx(S, '\\', '\', True); 
     Result := S; 
     Exit; 
     end; 
    end; { <--- Missing } 
    end; 
end;