51Testing软件测试论坛

标题: 一个调用windows api读写INI文件的类 [打印本页]

作者: gy21st    时间: 2007-11-21 15:17
标题: 一个调用windows api读写INI文件的类
对于类似格式如下的文件
[section]
key1=string1
key2=string2
      .
      .
      .

windows提供了读取和写入键值的API函数GetPrivateProfileString和WritePrivateProfileString
以下提供了QTP中调用windows API函数尽情操作类似INI文件的类。直接拷贝到QTP中可以运行

'====================================
Extern.Declare micInteger, "GetPrivateProfileStringA", "Kernel32.dll", "GetPrivateProfileStringA", micString, micString, micString, micString+micByRef, micInteger, micString
Extern.Declare micInteger, "WritePrivateProfileStringA", "Kernel32.dll", "WritePrivateProfileStringA", micString, micString, micString, micString

Class INIClass

        Private IniFileName
        Public ErrorMsg

        Private Sub Class_Initialize()
           IniFileName = vbNullString
           ErrorMsg = vbNullString
        End Sub

        Private Function IsINIFileExist()
                If IniFileName = vbNullString Then
                        ErrorMsg = "No INI file exist"
                        IsINIFileExist = False
                        Exit Function
                End If
                IsINIFileExist = True
        End Function
       
        Public Sub SetINIPath(strFilePathName)
                IniFileName = Trim(strFilePathName)
        End Sub
       
        Public Function WriteKey(Section, key, Value)
            WriteKey = False
                If Not IsINIFileExist() Then
                        Exit Function
                End If

                If CInt(Extern.WritePrivateProfileStringA(Section, key, Value, IniFileName)) = 0 Then
                        ErrorMsg = "Failed to write INI file"
                        Exit Function
                End If
               
                WriteKey = True
        End Function

        Public Function ReadKey(Section, key, Size)
                Dim strKeyValue
                ReadKey = vbNullString

                If Not IsINIFileExist() Then
                        Exit Function
                End If

                strKeyValue = Space(Size)
                If CInt(Extern.GetPrivateProfileStringA(Section, key, vbNullString, strKeyValue, Size, IniFileName)) > 0 Then
                        ReadKey = Trim(strKeyValue)
                Else
                        ErrorMsg = "Can't find the value for key(" & key & ") in Section [" & Section & "]."
                End If       

        End Function
       
End Class

'================================================

Set oINI = New INIClass

oINI.SetINIPath("C:\test.ini")
oINI.WriteKey "Section1", "key1", "value1"
oINI.WriteKey "Section1", "key2", "value2"
oINI.WriteKey "Section1", "key3", "value3"

oINI.WriteKey "Section2", "key1", "value4"
oINI.WriteKey "Section2", "key2", "value5"
oINI.WriteKey "Section2", "key3", "value6"

s1 = oINI.ReadKey("Section1", "key1", 10)
s2 = oINI.ReadKey("Section2", "key1", 10)
MsgBox s1
MsgBox s2

Set oINI = Nothing
作者: walker1020    时间: 2007-11-21 22:49
如果楼主能说一下什么时候 会用到上面这个文件 ,就更好了
作者: walker1020    时间: 2007-11-21 22:50
无论如何,都要感谢楼主的无私奉献
作者: walker1020    时间: 2007-11-21 22:51
记得曾经有朋友问过,如何用QTP测试键值,这下有救了
作者: ppent    时间: 2007-11-22 09:59
这是个好东东,谢谢楼主的分享,赶紧收藏。




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2