How do i read and write to a ini files?
Printable View
How do i read and write to a ini files?
Declare these two APIs in a module:
Public 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
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As String, ByVal lpFileName As String) As Long
To write to an ini file:
Dim Res As Long
Res = WritePrivateProfileString("Section", "Key Name", "Key Value", "C:\MyINI.ini")
"Res" will be zero if an error occurs.
To read:
Dim strString As String
Dim strStringLen As Integer
strString = Space(256)
strStringLen = 255
GetPrivateProfileString "Section", "Key Name", "", strString, strStringLen, "C:\MyINI.ini"
If "Section" or "Key Name" is not found then strString will be "" else strString will contain the value of "Key Name"
Hope this helps.
Regards:
VBJ
Have a look at:
http://www.vb-world.net/demos/ini/