Software solutions and questions blog
RSS icon Email icon Home icon
  • Werken met een inifile

    Posted on October 31st, 2008 Christophe 1 comment

    In het ‘oude’ visual basic werkte ik regelmatig met een inifile waarin veel settings stonden.
    Omdat ik af en toe wel bezoek over de vloer krijg met als zoektermen visual basic6 of bestanden + visual basic geef ik hier graag de werkwijze weer om gegevens van en naar een inifile te schrijven.

    Declaratie van functies:
    Declare Function GetPrivateProfileString Lib "kernel32" Alias _
    "GetPrivateProfileStringA" (ByVal lpApplicationName _
    As String, ByVal lpKeyName As Any, ByVal lpDefault _
    As String, ByVal lpReturnedString As String, ByVal _
    nSize As Long, ByVal lpFileName As String) As Long
    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

    Gegevens uit een inifile halen:

    Public Function sGetINI(sINIfile As String, sSection As String, sKey _
    As String, sDefault As String) As String

    Dim sTemp As String * 256
    Dim nLength As Integer

    sTemp = Space$(256)

    nLength = GetPrivateProfileString(sSection, sKey, sDefault, sTemp, _
    255, sINIfile)
    sGetINI = Left$(sTemp, nLength)

    End Function

    Gegevens naar een inifile schrijven
    Public Sub writeINI(sINIfile As String, sSection As String, sKey _
    As String, sValue As String)

    Dim n As Integer
    Dim sTemp As String

    sTemp = sValue

    'Replace any CR/LF characters with spaces
    For n = 1 To Len(sValue)
    If Mid$(sValue, n, 1) = vbCr Or Mid$(sValue, n, 1) = vbLf _
    Then Mid$(sValue, n) = " "
    Next n

    n = WritePrivateProfileString(sSection, sKey, sTemp, sINIfile)

    End Sub

     

    One response to “Werken met een inifile”

    1. Classic!
      In sommige projecten gebruik ik, noodgedwongen, nog steeds ini files.

    Leave a reply