Tipp 0496 Einlesen aller Sektionen einer INI-Datei
Autor/Einsender:
Datum:
  Detlev Schubert
27.05.2006
Entwicklungsumgebung:   VB 6
Mit den API-Funktionen GetPrivateProfileString sowie GetPrivateProfileSection lassen sich INI-Dateien sehr komfortabel auslesen.
Übergibt man der Funktion GetPrivateProfileString im 2. Parameter NULL (vbNullString), werden alle Sektionsnamen der INI-Datei mit dem Puffer lpReturnedString zurückgegeben. Da alle einzelnen Sektions-Namen mit einem Chr$(0) abgeschlossen sind, lassen sich mittels der Split-Funktion alle Namen einfach separieren und so wie in dem Beispiel in eine ListBox schreiben. Ganz ähnlich lässt sich GetPrivateProfileSection handhaben, wobei unter Windows 9x die Größe des zu lesenden Abschnitts 32 KB nicht überschreiten darf.
Nach der Übergabe des Sektionsnamen erhält man alle Schlüssel dieser Sektion mit den zugewiesenen Werten, die dann auch nur noch separiert werden brauchen. So lässt sich eine INI-Datei mit nur etwas mehr als 20 Codezeilen komfortabel auslesen und ausgeben.
 
Option Explicit

Private Declare Function GetPrivateProfileString Lib "kernel32" _
    Alias "GetPrivateProfileStringA" (ByVal sSectionName As _
    String, ByVal sKeyName As String, ByVal sDefault As String, _
    ByVal sReturnedString As String, ByVal lSize As Long, _
    ByVal sFileName As String) As Long

Private Declare Function GetPrivateProfileSection Lib "kernel32" _
    Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, _
    ByVal lpReturnedString As String, ByVal nSize As Long, _
    ByVal lpFileName As String) As Long

Dim nRetVal  As Long
Dim strValue As String
Dim lngX As Long
Dim strINIFileName As String

Private Sub Form_Load()
  Dim strSections() As String

  On Error Resume Next
  strINIFileName = ApplicationPath & "Test.ini"

  strValue = Space$(8192)
  nRetVal = GetPrivateProfileString(vbNullString, vbNullString, _
        vbNullString, strValue, Len(strValue), strINIFileName)

  If nRetVal <> 0 Then
    strSections() = Split(strValue, Chr$(0))
    For lngX = 0 To UBound(strSections()) - 2
      List1.AddItem strSections(lngX)
    Next
  End If
End Sub

Function ApplicationPath() As String
  ApplicationPath = App.Path & _
        IIf(Right$(App.Path, 1) = "\", "", "\")
End Function

Private Sub List1_Click()
  Dim strSections() As String
  Dim intX As Integer

  On Error Resume Next
  List2.Clear
  Text1.Text = ""
  strValue = Space$(16384)
  nRetVal = GetPrivateProfileSection(List1.List(List1.ListIndex), _
        strValue, Len(strValue), strINIFileName)

  If nRetVal <> 0 Then
    strSections() = Split(strValue, Chr$(0))
    For lngX = 0 To UBound(strSections()) - 2
      intX = InStr(1, strSections(lngX), "=")
      List2.AddItem Left$(strSections(lngX), intX - 1)
    Next
  End If
End Sub

Private Sub List2_Click()
  On Error Resume Next
  strValue = Space$(512)
  nRetVal = GetPrivateProfileString(List1.List(List1.ListIndex), _
        List2.List(List2.ListIndex), vbNullString, strValue, _
        Len(strValue), strINIFileName)
  If nRetVal <> 0 Then
    Text1.Text = Trim$(strValue)
  End If
End Sub
 
Links zum Thema
Programm-Einstellungen speichern (INI-Datei)

Windows-Version
95
98
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (3,4 kB) Downloads bisher: [ 1026 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Samstag, 27. August 2011