by Eddy B (3 Submissions)
Category: Files/File Controls/Input/Output
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 26th February 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Read and Write to an *.INI file
API Declarations
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
'************************************************************************
'DESCRIPTION: WRITES INFORMATION TO AN INI FILE
'INPUTS: SZSECTION, SZFIELD, SZVALUE
'OUTPUTS: WRITEINIFILE
'AUTHOR: EB
'DATE EDITED: 2/22/01
'************************************************************************
On Error GoTo Err_WriteINIFile
Dim strProcName As String
strProcName = "WriteINIFile"
Dim Success As Long
Success = WritePrivateProfileString(szSection, szField, szValue, App.Path + "\IniName.INI")
If Success = False Then
WriteINIFile = False
Else
WriteINIFile = True
End If
Exit_WriteINIFile:
Exit Function
Err_WriteINIFile:
MsgBox Err.Number & Err.Description & vbCrLf & strProcName, vbCritical
GoTo Exit_WriteINIFile
End Function
Public Function GetINIFile(ByVal szSection As String, ByVal szField As String)
'**********************************************************************
'DESCRIPTION: GETS INFORMATION FROM AN INI FILE
'INPUTS: SZSECTION, SZFIELD
'OUTPUTS: GETINIFILE
'AUTHOR: EB
'DATE EDITED: 2/22/01
'**********************************************************************
On Error GoTo Err_GetINIFile
Dim strProcName As String
Dim nRet As Integer
Dim szFileName As String
Dim szBuffer As String
Dim szDefault As String
Dim nTempLength As Integer
strProcName = "GetINIFile"
szDefault = ""
szBuffer = String(80, " ")
nRet = GetPrivateProfileString(szSection, szField, szDefault, szBuffer, Len(szBuffer), App.Path + "\IniName.INI")
If nRet > 0 Then
GetINIFile = Left(szBuffer, nRet)
Else
GetINIFile = szDefault
End If
Exit_GetINIFile:
Exit Function
Err_GetINIFile:
MsgBox Err.Number & Err.Description & vbCrLf & strProcName, vbCritical
GoTo Exit_GetINIFile
End Function