Ich hab' hier mal eben schnell ne Klasse zusammengestellt:
Public Class INI
' API-Deklarationen
Private Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As String, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer
Private Declare Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringA" ( _
ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Integer
Private m_IniFile As String = ""
Public Sub New(ByVal sIniFile As String)
m_IniFile = sIniFile
End Sub
''' <summary>
''' Liest einen Eintrag aus der INI-Datei aus
''' </summary>
Public Function ReadString(ByVal sSection As String, _
ByVal sKeyName As String, _
Optional ByVal sDefault As String = "")
Dim nResult As Integer
Dim sBuffer As String = Space$(1024)
Dim nSize As Integer = sBuffer.Length
nResult = GetPrivateProfileString(sSection, sKeyName, sDefault, _
sBuffer, nSize, m_IniFile)
If nResult > 0 Then
sBuffer = sBuffer.Substring(0, nResult)
If sBuffer.EndsWith(Chr(0)) Then sBuffer.Trim(Chr(0))
Return sBuffer
Else
Return sDefault
End If
End Function
''' <summary>
''' Speichert den angegeben String in die INI-Datei
''' </summary>
Public Sub WriteString(ByVal sSection As String, _
ByVal sKeyName As String, _
ByVal sString As String)
WritePrivateProfileString(sSection, sKeyName, sString, m_IniFile)
End Sub
End Class Hiermit kannst Du jetzt sehr einfach Einträe aus einer INI-Datei lesen und auch schreiben:
' Einträge speichern
With New INI("d:\myini.ini")
.WriteString("Netzwerk", "IP-Adresse", "192.168.0.1")
.WriteString("Netzwerk", "Subnet", "255.255.224.0")
End With' Einträge auslesen
With New INI("d:\myini.ini")
sIP = .ReadString("Netzwerk", "IP-Adresse", "192.168.0.1")
sSubnet = .ReadString("Netzwerk", "Subnet", "255.255.224.0")
End With_________________________
Professionelle Entwicklerkomponenten
www.tools4vb.de |