51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 4576|回复: 4
打印 上一主题 下一主题

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

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2007-11-21 15:17:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
对于类似格式如下的文件
[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
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

  • TA的每日心情
    开心
    2016-2-27 08:48
  • 签到天数: 2 天

    连续签到: 1 天

    [LV.1]测试小兵

    2#
    发表于 2007-11-21 22:49:23 | 只看该作者
    如果楼主能说一下什么时候 会用到上面这个文件 ,就更好了
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2016-2-27 08:48
  • 签到天数: 2 天

    连续签到: 1 天

    [LV.1]测试小兵

    3#
    发表于 2007-11-21 22:50:19 | 只看该作者
    无论如何,都要感谢楼主的无私奉献
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2016-2-27 08:48
  • 签到天数: 2 天

    连续签到: 1 天

    [LV.1]测试小兵

    4#
    发表于 2007-11-21 22:51:57 | 只看该作者
    记得曾经有朋友问过,如何用QTP测试键值,这下有救了
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    5#
    发表于 2007-11-22 09:59:07 | 只看该作者
    这是个好东东,谢谢楼主的分享,赶紧收藏。
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-2 19:22 , Processed in 0.078599 second(s), 27 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表