google搜索 51Testing站内搜索                    软件测试门户 | 软件测试培 训 | 文章资料精选 | 软件测试论坛 | 软件测试博客 | 测试招聘求职 
打印

[原创] 一个调用windows api读写INI文件的类

一个调用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

TOP

如果楼主能说一下什么时候 会用到上面这个文件 ,就更好了
实践是检验真理的唯一标准。

TOP

无论如何,都要感谢楼主的无私奉献
实践是检验真理的唯一标准。

TOP

记得曾经有朋友问过,如何用QTP测试键值,这下有救了
实践是检验真理的唯一标准。

TOP

这是个好东东,谢谢楼主的分享,赶紧收藏。
个人主页:扬起测试的风帆

TOP

 
当前时区 GMT+8, 现在时间是 2008-7-6 16:11Copyright(C)上海博为峰软件技术有限公司 2001-2007 电话:021-64471599-8017
当您在访问网站、论坛及博客过程中遇到问题时可发送email:webmaster@51testing.com或发送论坛短信至管理员风在吹