[Visual Basic]VB源码推荐:一个操作Ini文件的类

    技术2022-05-11  185

    【VB源码推荐:一个操作Ini文件的类】'--------cIniFile.cls 代码----------------'这里定义了一个cIniFile类Option Explicit'// Private member that holds a reference to'// the path of our ini filePrivate strInI As String'// Win API DeclaresPrivate Declare Function WritePrivateProfileString _  Lib "kernel32" Alias "WritePrivateProfileStringA" _(ByVal lpApplicationName As String, _  ByVal lpKeyName As Any, _  ByVal lpString As Any, _  ByVal lpFileName As String) As LongPrivate 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 LongPrivate Function MakePath(ByVal strDrv As String, _  ByVal strDir As String) As String'// Makes an INI file: Guarantees a sub dirDo While Right$(strDrv, 1) = "/"   strDrv = Left$(strDrv, Len(strDrv) - 1)LoopDo While Left$(strDir, 1) = "/"   strDir = Mid$(strDir, 2)Loop'// Return the pathMakePath = strDrv & "/" & strDirEnd FunctionPublic Sub CreateIni(strDrv As String, strDir As String)'// Make a new ini filestrInI = MakePath(strDrv, strDir)End SubPublic Sub WriteFile(strSection As String, _strKey As String, _strValue As String)'// Write to strINIWritePrivateProfileString strSection, _   strKey, strValue, strInIEnd SubPublic 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 = strTmpEnd FunctionPublic Property Let INIFile(ByVal New_IniPath As String)'// Sets the new ini pathstrInI = New_IniPathEnd PropertyPublic Property Get INIFile() As String'// Returns the current ini pathINIFile = strInIEnd Property'--------cIniFile.cls 使用举例----------------    Dim myIniFile As New cIniFile        '---指定访问的ini文件    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        '---写入ini文件     myIniFile.WriteFile "setting", "username", strUser       '---读出ini文件的数据     '    注意,如果是字符串,则去掉末尾一个字符     '                              ----flybird@chinaasp.com    strUser = Trim(myIniFile.GetFile("setting", "username"))    strUser = Left(strUser, Len(strUser) - 1)


    最新回复(0)