Attribute VB_Name = "ReadWriteINI"
Option Explicit
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 AppName$, ByVal KeyName$, ByVal keydefault$, ByVal FileName$)
Function GetKeyVal(ByVal INIFileLoc As String, ByVal Section As String, ByVal Key As String) As String
    'This Function retrieves information from an INI File
    'INIFileLoc = The location of the INI File (ex. "C:\Windows\INIFile.ini")
    'Section = Section where the Key is held
    'Key = The Key of which you want to retrieve information
    'Checking to see if the INI File specified exists
    If Dir(INIFileLoc) = "" Then MsgBox "File Not Found: " & INIFileLoc & vbCrLf & "Please refer To code in Function 'GetKeyVal'", vbExclamation, "INI File Not Found": Exit Function
    'If INI File exists then proceed to GetKey Value
    Dim RetVal As String, Worked As Integer
    RetVal = String$(255, 0)
    Worked = GetPrivateProfileString(Section, Key, "", RetVal, Len(RetVal), INIFileLoc)


    If Worked = 0 Then
        GetKeyVal = ""
    Else
        GetKeyVal = Left(RetVal, InStr(RetVal, Chr(0)) - 1)
    End If
End Function
Function AddToINI(ByVal INIFileLoc As String, ByVal Section As String, ByVal Key As String, ByVal Value As String)
    'This Function adds a Section, Key, or Value to an INI file
    'Also used to CREATE NEW INI FILE
    'INIFileLoc = The location of the INI File (ex. "C:\Windows\INIFile.ini")
    'Section = The name of the referred to Section or newly created Section (ex. "New Section 1")
    'Key = The name of the referred to Key or newly created Key (ex. "New Key 1")
    'Value = The value to hold in the givenKey (ex. "New Info Held")
    'Checking to see if the INI File specified exists
    If Dir(INIFileLoc) = "" Then MsgBox "File Not Found: " & INIFileLoc & vbCrLf & "Please refer To code in Function 'AddToINI'", vbExclamation, "INI File Not Found": Exit Function
    'If INI File exists then proceed to Addthe information to the INI File
    WritePrivateProfileString Section, Key, Value, INIFileLoc
End Function

Function DeleteSection(ByVal INIFileLoc As String, ByVal Section As String)
    'This Function Deletes a specified Section from an INI file
    'INIFileLoc = The location of the INI File (ex. "C:\Windows\INIFile.ini")
    'Section = The name of the Section you wish to remove (ex. "Section Number 1")
    'Checking to see if the INI File specified exists
    If Dir(INIFileLoc) = "" Then MsgBox "File Not Found: " & INIFileLoc & vbCrLf & "Please refer To code in Function 'DeleteSection'", vbExclamation, "INI File Not Found": Exit Function
    'If INI File exists then proceed to delete Section
    WritePrivateProfileString Section, vbNullString, vbNullString, INIFileLoc
    'NOTE: vbNullString is the coding in which to delete a Section, or Key
End Function


Function DeleteKey(ByVal INIFileLoc As String, ByVal Section As String, ByVal Key As String)
    'This Function Deletes a Key in a specified Section from an INI file
    'INIFileLoc = The location of the INI File (ex. "C:\Windows\INIFile.ini")
    'Section = The name of the Section in which the Key to be deleted is held (ex. "Section Number 1")
    'Key = The name of the Key you wish to remove (ex. "Key Number 5")
    'Checking to see if the INI File specified exists
    If Dir(INIFileLoc) = "" Then MsgBox "File Not Found: " & INIFileLoc & vbCrLf & "Please refer To code in Function 'DeleteKey'", vbExclamation, "INI File Not Found": Exit Function
    'If INI File exists then proceed to delete Key
    WritePrivateProfileString Section, Key, vbNullString, INIFileLoc
    'NOTE: vbNullString is the coding in which to delete a Section, or Key
End Function
Function DeleteKeyValue(ByVal INIFileLoc As String, ByVal Section As String, ByVal Key As String)
    'This Function deletes the value in a specified Key from an INI file
    'INIFileLoc = The location of the INI File (ex. "C:\Windows\INIFile.ini")
    'Section = The name of the Section in which the Key is held (ex. "Section Number1")
    'Key = The name of the Key you wish to remove the value from (ex. "Key Number 5")
    'Checking to see if the INI File specified exists
    If Dir(INIFileLoc) = "" Then MsgBox "File Not Found: " & INIFileLoc & vbCrLf & "Please refer To code in Function 'DeleteKeyValue'", vbExclamation, "INI File Not Found": Exit Function
    'If INI File exists then proceed to delete Key Value
    WritePrivateProfileString Section, Key, "", INIFileLoc
    ' "" = is a short way of saying Nothing
End Function
Function RenameSection()
    'Coming Soon
End Function
Function RenameKey()
    'Coming Soon
End Function


