Folge dem Video um zu sehen, wie unsere Website als Web-App auf dem Startbildschirm installiert werden kann.
Anmerkung: Diese Funktion ist in einigen Browsern möglicherweise nicht verfügbar.
Option Explicit
Dim fso
Main
Set fso = Nothing
Sub Main
Dim oFile
Dim oINI
Dim oSection
Dim sFileContent
Const ForReading = 1
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.OpenTextFile("C:\Temp\Test.ini", ForReading)
sFileContent = oFile.ReadAll
oFile.Close
Set oFile = Nothing
Set oINI = New INIFile
oINI.Parse sFileContent
'Hier sollte man wissen, dass der Bereich existieren, sonst Fehler
With oINI.SectionByName("TestBereich")
MsgBox .ValueByName("Schlüssel")
End With
'Hier sollte man wissen, dass der Bereich existieren, sonst Fehler
With oINI("TestBereich")
MsgBox .ValueByName("Schlüssel")
End With
Set oSection = oINI("TestBereich")
If Not oSection Is Nothing Then
'Existiert der Schlüssel nicht->Leerzeichenfolge
MsgBox oSection("Schlüssel")
End If
'Aufräumen
Set oINI = Nothing
End Sub
Class INIFile
Private mv_aSections
Private Sub Class_Initialize()
mv_aSections = Array()
End Sub
Private Sub Class_Terminate()
Erase mv_aSections
End Sub
'Übergabe in VBScript mit ByRef. Ab VB.Net mit ByVal!
Public Sub Parse(ByRef sFileContent)
Dim i
Dim aSections
Dim aSectionData
aSections = Split(vbCRLF & sFileContent, vbCRLF & "[")
ReDim mv_aSections(UBound(aSections, 1))
For i = 0 To UBound(aSections, 1)
Set mv_aSections(i) = New INISection
With mv_aSections(i)
aSectionData = Split(aSections(i), "]" & vbCRLF, 2)
.Name = aSectionData(0)
.Parse aSectionData(1)
Erase aSectionData
End With
Next
End Sub
Public Default Property Get SectionByName(sName)
Dim i
For i = 0 To UBound(mv_aSections, 1)
If StrComp(mv_aSections(i).Name, sName, vbTextCompare) = 0 Then
Set SectionByName = mv_aSections(i)
Exit Property
End If
Next
Set SectionByName = Nothing
End If
End Class
Class INISection
Public Name
Private mv_aKeys
Private Sub Class_Initialize()
Name = ""
mv_aKeys = Array()
End Sub
Private Sub Class_Terminate()
Erase mv_aKeys
End Sub
Public Sub Parse(ByRef sSectionData)
Dim i
Dim aLines
Dim aKey
If Len(sSectionData) = 0 Then Exit Sub
aLines = Split(aSectionData, vbCRLF)
ReDim mv_aKeys(UBound(aLines, 1))
On Error Resume Next
For i = 0 To UBound(aLines, 1)
Set mv_aKeys(i) = New INIKey
Select Case True
Case Len(aLines(i) = 0
mv_aKeys(i).Type = 0
Case Left(aLines(i), 1) = ";", LCase(Left(aLines(i), 3)) = "rem"
With mv_aKeys(i)
.Type = 2
.Value = aLines(i)
End With
Case Else
aKey = Split(aLines(i), "=", 2)
With mv_aKeys(i)
.Type = 1
.Name = aKey(0)
.Value = aKey(1)
End With
Erase aKey
End Select
Next
On Error Goto 0
End Sub
' Zugriff über Namen (Erstes Vorkommen)
Public Default Property Get ValueByName(sKey)
Dim i
For i = 0 To UBound(mv_aKeys, 1)
With mv_aKeys(i)
If .Type = 2 Then
If StrComp(.Name, sKey) = 0 Then
ValueByName = .Value
Exit Property
End If
End If
End With
Next
ValueByName = ""
End Property
End Class
Class INIKey
Public Name
Public Value
Public Type '0 = Leer, 1 = Key, 2 = Kommentar
End Class