2015-10-04 55 views
1

我目前正在研究一個遊戲啓動器,並從互聯網借用的工作代碼能夠解析一個INI。所有的工作都很好地保存了一個問題。VBScript,HTA - INI分析,允許內嵌評論

它無法解析ini文件中的內嵌評論。

例子:

[Window] 
Width=800 

解析罰款,沒有問題,很好。

[Window] 
Width=800 ; width in pixels 

但是上面沒有,我需要它能夠停止讀取線在檢測;如果可能的話。

這裏是我的全部HTA代碼:

<Html> 
<Head> 

    <Title>Installer</Title> 
    <Meta Http-Equiv="x-ua-compatible" Content="ie=9"> 
    <Link Rel="stylesheet" Type="text/css" Href="image/appStyles.css" Media="screen" /> 

    <Script Language="VBScript" Type="Text/VBScript"> 
    '-- Scripts to be carried out before the installer loads in. 

    '-- Functions --' 

    Function ReadIni(myFilePath, mySection, myKey) 
    ' This function returns a value read from an INI file 

    ' Examples 
    ' ReadIni("settings.config", "Section1", "Keyname1") 
    ' ReadIni("settings.config", "Section1", "Keyname2") 
    ' ReadIni("settings.config", "Section2", "Keyname1") 
    ' ReadIni("settings.config", "Section4", "Keyname2") 

    Const ForReading = 1 
    Const ForWriting = 2 
    Const ForAppending = 8 

    Dim intEqualPos 
    Dim objFSO, objIniFile 
    Dim strFilePath, strKey, strLeftString, strLine, strSection 

    Set objFSO = CreateObject("Scripting.FileSystemObject") 

    ReadIni  = "" 
    strFilePath = Trim(myFilePath) 
    strSection = Trim(mySection) 
    strKey  = Trim(myKey) 

    If objFSO.FileExists(strFilePath) Then 
     Set objIniFile = objFSO.OpenTextFile(strFilePath, ForReading, False) 
     Do While objIniFile.AtEndOfStream = False 
      strLine = Trim(objIniFile.ReadLine) 

     ' Check if section is found in the current line 
     If LCase(strLine) = "[" & LCase(strSection) & "]" Then 
      strLine = Trim(objIniFile.ReadLine) 

     ' Parse lines until the next section is reached 
      Do While Left(strLine, 1) <> "[" 

     ' Find position of equal sign in the line 
      intEqualPos = InStr(1, strLine, "=", 1) 

     If intEqualPos > 0 Then 
      strLeftString = Trim(Left(strLine, intEqualPos - 1)) 

     ' Check if item is found in the current line 
      If LCase(strLeftString) = LCase(strKey) Then 
       ReadIni = Trim(Mid(strLine, intEqualPos + 1)) 

       ' In case the item exists but value is blank 
       If ReadIni = "" Then 
        ReadIni = " " 
       End If 

       ' Abort loop when item is found 
        Exit Do 
       End If 
      End If 

      ' Abort if the end of the INI file is reached 
       If objIniFile.AtEndOfStream Then Exit Do 

      ' Continue with next line 
       strLine = Trim(objIniFile.ReadLine) 
       Loop 
        Exit Do 
       End If 
     Loop 
      objIniFile.Close 
     Else 
      ' WScript.Echo strFilePath & " doesn't exists. Exiting..." 
      ' Wscript.Quit 1 
     End If 
    End Function 


    '-- Subroutines --' 

    '-- Resize & move app to center 
    Sub SetWindow(WidthX,HeightY) 
     Self.ResizeTo WidthX, HeightY 
     Self.MoveTo (screen.Width - WidthX)/2, (screen.Height - HeightY)/2 
    End Sub 

    '-- Close app 
    Sub WinClose 
     Self.Close 
    End Sub 


    '-- Startup --' 

    '-- Read the configuration settings. 

    IniFile = "settings.config" 
    WinWidth = ReadIni(IniFile, "Window", "Width") 
    WinHeight = ReadIni(IniFile, "Window", "Height") 

    '-- Set Window size 
    SetWindow WinWidth, WinHeight 


    </Script> 

    <Hta:Application Id="Installer" ApplicationName="Installer"  Version="0.1" 

     SingleInstance="Yes" 
     Icon="image/appIcon.ico" 
     Caption="No" 
     Border="None" 
     InnerBorder="No" 
     ContextMenu="No" 
     SysMenu="None" 
     Scroll="No" 
     Selection="No" 

    /> 

</Head> 

<Body> 

    <Div Id="status">Hello</Div> 


<Script Language="VBScript" Type="Text/VBScript"> 
'-- Scripts that require access to the DOM... 

    '-- Startup 
    document.getElementById("status").InnerHTML = "Idle" 
    document.title = ReadIni(IniFile, "App", "Title") 


</Script> 

<Script Type="Text/Javascript"> 
//-- Javascripts that require access to the DOM... 

    window.onload = function() { 
     var a = document.getElementsByTagName("img"); 
      a.ondragstart = function() { return false; } 
    } 
</Script> 

</Body> 
</Html> 

任何幫助,你們可以提供將是巨大的,謝謝!

+1

不完全相關,但JS部分將無法正常工作。 'getElementsByTagName()'返回一個HTMLCollection,它是一個類似數組的對象。你必須遍歷它來訪問圖像元素,集合本身沒有'ondragstart'方法。 – Teemu

+0

哦,是的,我正在修補它,將它應用於任何圖像,但意識到我不打算與發射器內的多個圖像(將要使用圖像的一個關閉圖標),但決定只是使用CSS來代替。因此,我將其更改回爲'var a = document.getElementById(「Logo」);' – Ctrlaltdenied

回答

0
WinWidth = Trim(Split(ReadIni(IniFile, "Window", "Width"), ";")(0)) 

分割使用分號值,取第一個元素的列表,並刪除開始/結束場所如果存在

+0

非常感謝,但這是編輯讀取INI的代碼,而不是實際進行讀取的代碼。可以編碼以防止在一行之後讀取行;不能在ReadIni函數內完成?感謝您迄今的努力。 – Ctrlaltdenied

+1

@Ctrlaltdenied,你可以在'ReadIni'函數內放置'Split(..'),但是你可能會遇到一個問題:如何讀取一個可能包含分號的值?在這種情況下,我決定去做'Split'在返回值上,而不是在獲取行內容的代碼中 –

+0

好的,謝謝澄清,ini文件中不會有任何分號用於值,它只是在那裏讀取設置,啓動器/安裝程序是可配置的,我只是希望能夠爲其用戶提供個人價值的內聯評論,而不是將註釋作爲單獨的行來進行評論。目前主要問題是,如果;在值,它會導致值被錯誤地讀取,並導致應用程序失敗。 – Ctrlaltdenied