How-To... - Saving/Retrieving Program Information Using INI Files.

INI Files.......Even though using the registry for program settings and information, INI files are still very useful. Windows 3.11, especially, uses INI files for most of its programs because there is no registry. As well as being used in Windows 3.11, people prefer using them because they are safer than accessing the registry.

Well, anyway onto the code.

Start a new project and insert a standard module. Insert the following code into the General Declerations of the module.

' Below function is used to READ from INI file(All One Line)
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

' Below function is used to WRITE to INI file(All One Line)
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

'<--END OF MOD CODE-->

Now, on the form, put on three buttons called cmdWrite, cmdReadIni and cmdKillIni respectively. The Form name is frmIniFiles. Paste the following code into the General Declerations of the Form :-

'Below are variables used for the INI file
'reading and writing.....

Dim lpAppName As String
Dim lpKeyName As String
Dim lpBuffer As String
Dim nSize As Long
Dim lpDefault As String
Dim lpFileName As String
Dim ret As Long

Private Sub cmdDelIni_Click()
'Deletes the INI file...
Kill lpFileName
End Sub

Private Sub cmdReadIni_Click()
'lpBuffer is what will be read from the file.
'The 255 makes sure that only the first 255
'characters will be read. A line in your INI file
'shouldn't be that long anyway...

lpBuffer = Space(255)
nSize = 255

'The below line reads the information from the file
'using the 6 INI file variables and stores the info
'read into the lpBuffer variable.

ret = GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, lpBuffer, nSize, lpFileName)

'Displays the information read as the Forms Caption...
frmIniFiles.Caption = lpBuffer
End Sub

Private Sub cmdWrite_Click()
Dim sData As String 'Create a variable....

'Store the text and Todays date in the variable...
sData = "Todays Date Is " & Date$

lpBuffer = sData 'Store the variable sData in lpBuffer

'The below line writes the information to the INI file
'using 4 of the 6 INI file variables. lpBuffer is always
'the data that is going to be written to the INI file.

ret = WritePrivateProfileString(lpAppName, lpKeyName, lpBuffer, lpFileName)

End Sub

Private Sub Form_Load()
'First off all the file needs to be created.
'Always, when writing to INI files, the file
'be already be created. The INI file used
'here will be created when the Form loads.
'But, if it already exists, then don't create
'it. (Uses the Dir Statement)

'Store vbhowto.ini if the file already exists, else
'nothing will be stored....

temp$ = Dir("C:\vbhowto.ini")

'If temp$ is greater or less than vbhowto.ini then
'If it is greater or less, this means the file does
'not exist...

If temp$ <> "vbhowto.ini" Then

'Creates the file and opens it for output
'Just means that the file is open for
'the program to write to it...

Open "C:\vbhowto.ini" For Output As #1

'Print [VbHowTo] to the file...
Print #1, "[VB How-To]"

'Print Message to the file...
Print #1, "Message="

Close #1 'Close the file...

End If

'Below line centres form in centre of screen.....
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2

'Fill the INI variables with the appropriate info...
lpAppName = "VB How-To" 'The section of the INI...
lpKeyName = "Message" 'The line in the section of the INI...
lpFileName = "C:\VbHowTo.ini" 'The location of the INI...

End Sub

You can download the fully-documented Project file from here.


This site is Copyright © 1999 of David Cowan. VB How-To, Dodgesoft and DodgeSoft Productions is Copyright © 1999 of David Cowan. Source-code and programs are copyright of their respective owners. For a messageboard, click here