'[VB Source Code Recommendation: A class that operates the INI file]
'-------- CIniFile. cls code ----------------
'A cIniFile class is defined here.
Option Explicit
'// Private member that holds a reference
'// The path of our ini file
Private strInI As String
'// Win API Declares
Private 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
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 Long ,_
ByVal lpFileName As String) As Long
Private Function MakePath (ByVal strDrv As String ,_
ByVal strDir As String) As String
'// Makes an INI file: Guarantees a sub dir
Do While Right $ (strDrv, 1) = ""
StrDrv = Left $ (strDrv, Len (strDrv)-1)
Loop
Do While Left $ (strDir, 1) = ""
StrDir = Mid $ (strDir, 2)
Loop
'// Return the path
MakePath = strDrv & "& strDir
End Function
Public Sub CreateIni (strDrv As String, strDir As String)
'// Make a new ini file
StrInI = MakePath (strDrv, strDir)
End Sub
Public Sub WriteFile (strSection As String ,_
StrKey As String ,_
StrValue As String)
'// Write to strINI
WritePrivateProfileString strSection ,_
StrKey, strValue, strInI
End Sub
Public Function GetFile (strSection As String ,_
StrKey As String) As String
Dim strTmp As String
Dim lngRet As String
StrTmp = String $ (100, Chr (32 ))
LngRet = GetPrivateProfileString (strSection ,_
StrKey, "", strTmp ,_
Len (strTmp), strInI)
GetFile = strTmp
End Function
Public property let INIFILE (byval new_inipath as string)
'// Sets the new ini path
Strini = new_inipath
End Property
Public property get INIFILE () as string
'// Returns the current ini path
INIFILE = Strini
End Property
'-------- Cinifile. CLs example ----------------
Dim myinifile as new cinifile
'--- Specifies the INI file to be accessed
If Len (App. Path)> 3 then
'Under disk root Dir, eg: "C :"
Myinifile. INIFILE = app. Path & "setting. ini"
Else
Myinifile. INIFILE = app. Path & "setting. ini"
End if
'--- Write the INI File
Myinifile. writefile "setting", "username", struser
'--- Read the INI file data
'Note: if it is a string, remove the last character.
'---- Flybird@chinaasp.com
Struser = trim (myinifile. GetFile ("setting", "username "))
Struser = left (struser, Len (struser)-1)