A class that operates the INI File

Source: Internet
Author: User
'[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)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.