# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  VB6 - READING & WRITING TO .INI

## Paul M

VB Code:
Private 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
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
Public Sub WriteINI(wiSection As String, wiKey As String, wiValue As String, wiFile As String)
    WritePrivateProfileString wiSection, wiKey, wiValue, App.Path & "\" & wiFile
End Sub
Public Function ReadINI(riSection As String, riKey As String, riFile As String, riDefault As String)
    Dim sRiBuffer As String
    Dim sRiValue As String
    Dim sRiLong As String
    Dim INIFile As String
    INIFile = App.Path & "\" & riFile
    If Dir(INIFile) <> "" Then
        sRiBuffer = String(255, vbNull)
        sRiLong = GetPrivateProfileString(riSection, riKey, Chr(1), sRiBuffer, 255, INIFile)
        If Left$(sRiBuffer, 1) <> Chr(1) Then
            sRiValue = Left$(sRiBuffer, sRiLong)
            If sRiValue <> "" Then
                ReadINI = sRiValue
            Else
                ReadINI = riDefault
            End If
        Else
            ReadINI = riDefault
        End If
    Else
        ReadINI = riDefault
    End If
End Function

----------


## bushmobile

there are already plenty of other (and better) INI handling examples in this forum (1, 2, 3, 4, 5, 6) - i suggest you search next time before posting, as per the codebank guidelines

----------


## Paul M

Lol i posted this because i made it and i don't like copy and pasting **** from others. But thanks for the tip.

----------

