trevorm
asked on
INI Files - Storing & Retrieving Info
I want to store the path of a database in the INI file of a program.
What is the best way to Create an entry in an INI file to store the database path. What is the best way to then retrieve this from the INI file.
What is the best way to Create an entry in an INI file to store the database path. What is the best way to then retrieve this from the INI file.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Option Explicit
#If Win16 Then
Private Declare Function GetPrivateProfileInt Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Integer, ByVal lpFileName As String) As Integer
Private Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Private Declare Function WritePrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lplFileName As String) As Integer
#Else
Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA"
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileString
#End If
Public Function ReadINI_Integer(szAppName As String, szKeyname As String, szININame As String, Optional varDefault) As Integer
Dim iResult As Integer
iResult = GetPrivateProfileInt(szApp
ReadINI_Integer = iResult
End Function
Public Function ReadINI_String(szAppName As String, szKeyname As String, szININame As String, Optional varDefault) As String
Const c_iLargest As Integer = 255
Dim szReturn As String
Dim iSizeReturn As Integer
Dim szDefault As String
If IsMissing(varDefault) Then
szDefault = ""
Else
szDefault = CStr(varDefault)
End If
szReturn = Space$(c_iLargest)
'Read from the INI File
iSizeReturn = GetPrivateProfileString(sz
'trim Read value and removed NULL
szReturn = Trim$(Left$(szReturn, iSizeReturn))
ReadINI_String = szReturn
End Function
Public Function WriteINI(szAppName As String, szKeyname As String, szValue As String, szININame As String)
WriteINI = WritePrivateProfileString(
End Function
Here are example functions on how to use them. These functions read/write the window coordinates:
Public Sub GetWindowPosition(frmCur As Form, szINIFile As String)
Dim iLeft As Integer
Dim iTop As Integer
Dim iHeight As Integer
Dim iWidth As Integer
iLeft = ReadINI_Integer(frmCur.Nam
If iLeft > 0 Then
frmCur.Left = iLeft
End If
iTop = ReadINI_Integer(frmCur.Nam
If iTop > 0 Then
frmCur.Top = iTop
End If
iHeight = ReadINI_Integer(frmCur.Nam
If iHeight > 0 Then
frmCur.Height = iHeight
End If
iWidth = ReadINI_Integer(frmCur.Nam
If iWidth > 0 Then
frmCur.Width = iWidth
End If
End Sub
Public Sub SaveWindowPosition(frmCur As Form, szINIFile As String)
WriteINI frmCur.Name, "Left", frmCur.Left, szINIFile
WriteINI frmCur.Name, "Top", frmCur.Top, szINIFile
WriteINI frmCur.Name, "Width", frmCur.Width, szINIFile
WriteINI frmCur.Name, "Height", frmCur.Height, szINIFile
End Sub