2017-08-21 63 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;