Dear all,
This FAQ explains how to Read and Write an INI files. To give a backgruond for newbies; INI files are Information files and are used to store some information required frequently... The same is accessed by some API's
in a new class module write the following code
in a form use the following declarations
to read from an ini file ; myini.ini which contains -
[first_ini]
idea=good
code=simple
[second_ini]
usage=frequent
U need to know the nomenclature also
"[first_ini]" is called a Section
"idea" is called a Key
"good" is called a Value
you can have 'n' number of these.....
- To read section key use
- To Write to a section key use
use the GetSection function to get all the possible Sections in a file.
Hope this helps .... lemme know
![[cheers] [cheers] [cheers]](/data/assets/smilies/cheers.gif)
Niraj
![[noevil] [noevil] [noevil]](/data/assets/smilies/noevil.gif)
This FAQ explains how to Read and Write an INI files. To give a backgruond for newbies; INI files are Information files and are used to store some information required frequently... The same is accessed by some API's
in a new class module write the following code
Code:
Option Explicit
' --------
' Public
' --------
'
' Property for file to read
Public File As String
' ---------
' Private
' ---------
'
' API to read/write ini's
#If Win32 Then
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 Integer, ByVal lpFileName As String) As Integer
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal Filename As String) As Integer
#Else
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 Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal Filename As String) As Integer
#End If
Sub DeleteSection(ByVal Section As String)
Dim retval As Integer
retval = WritePrivateProfileString(Section, 0&, "", File)
End Sub
Public Function SaveSetting(ByVal Section$, ByVal Key$, ByVal Value$)
Dim retval As Integer
SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File)
End Function
Public Function GetSetting(ByVal Section As String, ByVal KeyName As String) As String
Dim retval As Integer
Dim t As String * 255
' Get the value
retval = GetPrivateProfileString(Section, KeyName, "unknown value", t, Len(t), File)
' If there is one, return it
If retval > 0 Then
GetSetting = Left$(t, retval)
Else
GetSetting = "Unknown section or key"
End If
End Function
Public Function GetSection(ByVal Section As String, KeyArray() As String) As Integer
Dim retval As Integer
' Allocate space for return value
Dim t As String * 2500
Dim lastpointer As Integer
Dim nullpointer As Integer
Dim ArrayCount As Integer
Dim keystring As String
ReDim KeyArray(0)
' Get the value
retval = GetPrivateProfileString(Section, 0&, "", t, Len(t), File)
' If there is one, return it
If retval > 0 Then
'
' Separate the keys and store them in the array
nullpointer = InStr(t, Chr$(0))
lastpointer = 1
Do While (nullpointer <> 0 And nullpointer > lastpointer + 1)
'
' Extract key string
keystring = Mid$(t, lastpointer, nullpointer - lastpointer)
'
' Now add to array
ArrayCount = ArrayCount + 1
ReDim Preserve KeyArray(ArrayCount)
KeyArray(ArrayCount) = keystring
'
' Find next null
lastpointer = nullpointer + 1
nullpointer = InStr(nullpointer + 1, t, Chr$(0))
Loop
End If
'
' Return the number of array elements
GetSection = ArrayCount
End Function
in a form use the following declarations
Code:
Dim objIni As New iniClass
[first_ini]
idea=good
code=simple
[second_ini]
usage=frequent
U need to know the nomenclature also
"[first_ini]" is called a Section
"idea" is called a Key
"good" is called a Value
you can have 'n' number of these.....
- To read section key use
Code:
strSEc1 = objIni.GetSetting("frist_ini", "idea")
Code:
objIni.SaveSetting "frist_ini", "idea", "very_good"
use the GetSection function to get all the possible Sections in a file.
Hope this helps .... lemme know
![[cheers] [cheers] [cheers]](/data/assets/smilies/cheers.gif)
Niraj
![[noevil] [noevil] [noevil]](/data/assets/smilies/noevil.gif)