Visual Basic Programming Code Examples
Visual Basic > Files Directories Drives Code Examples
Using INI-files with your application
Using INI-files with your application
using INI-files (vb3)
'on a module
Option Explicit
Declare Function getprivateprofilestring Lib "Kernel" (ByVal SName$, ByVal KName$, ByVal def$, ByVal ret$, ByVal size%, ByVal inifilename$) as Integer
Declare Function writeprivateprofilestring Lib "Kernel" (ByVal SName$, ByVal KName$, ByVal SValue$, ByVal inifilename$) as Integer
'in the general section of the form
Dim inifile$
Dim UserName$
Sub Form_load
'give youre ini file the same name and location as youre executable only the extensie is different
inifile = app.Path & "\" & app.EXEName & ".ini"
'or if you want use the default windows-directory
'inifile = app.EXEName & ".ini"
'rest of code
End sub
Sub ReadIniFile
'This will look for the sector [options]
'and the entry 'name'
'the value will be written into the variable UserName$
dim succes#, t%
dim SName$, KName$, ret$
SName = "options"
KName = "name"
ret = String(255, 0)
succes = getprivateprofilestring(SName, KName, "", ret, Len(ret), inifile)
If succes Then UserName$ = Left$(ret, t% - 1)
End Sub
Sub WriteIniFile
'this will write the value of the variable UserName
'behind the entry 'name'
'under teh section [options]
dim succes%
dim SName$, KName$
SName = "options"
KName = "name"
succes = writeprivateprofilestring(SName, KName, UserName$ , inifile)
End sub
'using the WriteIniFile will make the sector and entry even if they don't exist.
'So just an errochecking in the ReadIniFile ;-)
'in case success = False
Return