Ik heb een voorbeeld gemaakt die demonstreert hoe een Inifile werkt.
Maak hierbij gebruik van een Classe
Heb hier op het forum al eens een simpele inifile Classe geplaatst.
Deze is uitgebreider zowel de Classe als het voorbeeld.
Mochten er vragen zijn hoor ik het wel
Code:
'Classe voorbeeld: IniFile
'Datum: Juli 2010
'Mr. Magoo
'www.worksheet.nl/forumexcel/
Option Explicit
Private Path_Property As String
Private Key_Property As String
Private Section_Property As String
Private Default_Property As String
Private WriteError_Property As Long
'Profile String DLL functie's:
Private Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As Any, ByVal lpKeyName As Any, ByVal lpDefault As Any, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
'Geef path en de filenaam op van de Inifile
Property Let Path(LetValue As String)
Path_Property = LetValue
End Property
'Geef path en de filenaam terug van de Inifile waar naar toe wordt geschreven
Property Get Path() As String
Path = Path_Property
End Property
'Geeft Error foutwaarde als het schrijven naar de Inifile is mislukt
'True -> Fout / False -> Geen fouten
Property Get WriteError() As Boolean
WriteError = Not (WriteError_Property > 0)
End Property
Property Let Default(LetValue As String)
Default_Property = LetValue
End Property
Property Get Default() As String
Default = Default_Property
End Property
'Keywaarde welke geschreven moet worden
Property Let Key(LetValue As String)
Key_Property = LetValue
End Property
'Lees de laatste gelezen keywaarde / keywaarde welke opgegeven is
Property Get Key() As String
Key = Key_Property
End Property
'Sectionwaarde welke geschreven moet worden
Property Let Section(LetValue As String)
Section_Property = LetValue
End Property
'Lees de laatste gelezen sectionwaarde / sectionwaarde welke opgegeven is
Property Get Section() As String
Section = Section_Property
End Property
'Lees de waarde uit de opgegeven Section en opgegeven Key
Property Get Value() As String
Dim ReturnValue As String, CountChar As Long
If Path_Property <> "" Then
ReturnValue = String(255, Chr(0))
CountChar = GetPrivateProfileString(Section_Property, Key_Property, Default_Property, ReturnValue, Len(ReturnValue), Path_Property)
End If
Value = IIf(CountChar > 0, Left(ReturnValue, CountChar), "")
End Property
'Schrijf de opgegeven waarde in de opgegeven Section en opgegeven Key
Property Let Value(LetValue As String)
WriteError_Property = WritePrivateProfileString(Section_Property, Key_Property, LetValue, Path_Property)
End Property
'Verwijder de key + keywaarde in een section
Public Sub DeleteKey()
WriteError_Property = WritePrivateProfileString(Section_Property, Key_Property, 0&, Path_Property)
End Sub
'Verwijder de opgegeven Section + key + keywaarde
Public Sub DeleteSection()
WriteError_Property = WritePrivateProfileString(Section_Property, 0&, 0&, Path_Property)
End Sub
'Schrijf huidige section naar Inifile
Property Let CurrentSection(LetValue As String)
WriteError_Property = WritePrivateProfileString(Section_Property, 0&, LetValue, Path_Property)
End Property
'Lees alle sections uit de Inifile en plaats deze in een string.
'Sections worden gescheiden door karakter chr(0)
Property Get AllSections() As String
Dim Buf As String, Size As String, CountChar As Long
If Path_Property <> "" Then
Size = 8192 '8 Kb (1024*8)
Buf = String(Size, Chr(0))
CountChar = GetPrivateProfileString(0&, 0&, Default_Property, Buf, Size, Path_Property)
End If
AllSections = IIf(CountChar > 0, Left(Buf, CountChar), "")
End Property
'Plaats alle sections in een array
Public Sub EnumerateAllSections(ByRef Section() As String, ByRef Count As Long)
Dim Sections As String
Sections = AllSections
Section() = Split(Sections, Chr(0))
Count = IIf(Len(Sections) > 0, UBound(Section()), 0)
End Sub
'Lees alle keys van huidige section uit de Inifile en plaats deze in een string.
'Keys worden gescheiden door karakter chr(0)
Property Get CurrentSection() As String
Dim Buf As String, Size As String, CountChar As Long
If Path_Property <> "" Then
Size = 8192 '8 Kb (1024*8)
Buf = String(Size, Chr(0))
CountChar = GetPrivateProfileString(Section_Property, 0&, Default_Property, Buf, Size, Path_Property)
End If
CurrentSection = IIf(CountChar > 0, Left(Buf, CountChar), "")
End Property
'Plaats alle keys van huidige section in een array
Public Sub EnumerateCurrentSection(ByRef Key() As String, ByRef Count As Long)
Dim Section As String
Section = CurrentSection
Key() = Split(Section, Chr(0))
Count = IIf(Len(Section) > 0, UBound(Key()), 0)
End Sub