VB6.0 实现读取ini配置文件

  1. 目标,让程序可以读取ini配置文件,根据其中配置信息,控制程序运行。

  2. 实现方式:让ini配置文件和程序在同一路径下,ini配置文件名固定,程序根据自己的位置+ini配置文件名来确定ini文件位置进行读取

  3. 实现步骤:

    1)添加一个公用模块,代码如下,这个可以理解为java中我们自己写的供调用的一个工具类

    Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
     
    Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, 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 Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
     
    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 IniFileName As String

    Public ErrorMsg As String
    '初始化函数
    Private Sub Class_Initialize()
         IniFileName = vbNullString
         ErrorMsg = vbNullString
    End Sub
    '设置ini配置文件位置及名称
    Public Sub SpecifyIni(FilePathName)
         IniFileName = Trim(FilePathName)
    End Sub
    '验证是否设置ini配置文件和配置文件是否存在
    Private Function NoIniFile() As Boolean
         NoIniFile = True
         If IniFileName = vbNullString Then
             ErrorMsg = "没有指定 INI 文件"
             Exit Function
         Else
            If Dir(IniFileName) = "" Then
                ErrorMsg = "未找到配置文件,请确认配置文件路径和名称"
                Exit Function
            End If
         End If
         ErrorMsg = vbNullString
         NoIniFile = False
    End Function
    '写操作(暂未使用)
    Public Function WriteString(Section As String, key As String, Value As String) As Boolean
         WriteString = False
         If NoIniFile() Then
             Exit Function
         End If
         If WritePrivateProfileString(Section, key, Value, IniFileName) = 0 Then
             ErrorMsg = "写入失败"
             Exit Function
         End If
         WriteString = True
    End Function
    '读操作
    Public Function ReadString(Section As String, key As String, Size As Long) As String
         Dim ReturnStr As String
         Dim ReturnLng As Long
         ReadString = vbNullString
         If NoIniFile() Then
             Exit Function
         End If
         ReturnStr = Space(Size)
         ReturnLng = GetPrivateProfileString(Section, key, vbNullString, ReturnStr, Size, IniFileName)
         ReadString = Left(ReturnStr, ReturnLng)
    End Function
    2)工具类写完以后,就可以去相应的form里调用了,以一个function为例,代码如下

     Function getSheetName() As String
        Dim curPath As String
        'ini配置文件和程序要在同一路径,并且配置文件名为sheetCof.ini,不可以改动
        curPath = App.Path & "\" & "sheetCof.ini"
        Call SpecifyIni(curPath)
        Dim confInfo As String
        confInfo = ReadString("sheetConf", "Key", 1000) '读取配置信息
        If confInfo = "" Then
            MsgBox ErrorMsg
        Else
           '在这里就可以利用读出来的配置信息进行操作了,这个例子只是读出了配置文件中sheetConf下的键值为“Key”的值
        End If
     End Function

     

你可能感兴趣的:(ini,vb6.0)