• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
    • 軟件測試技術
    • 軟件測試博客
    • 軟件測試視頻
    • 開源軟件測試技術
    • 軟件測試論壇
    • 軟件測試沙龍
    • 軟件測試資料下載
    • 軟件測試雜志
    • 軟件測試人才招聘
      暫時沒有公告

    字號: | 推薦給好友 上一篇 | 下一篇

    一個絕對經典的在VB中操作.ini文件的通用類源代碼

    發布: 2007-7-14 20:28 | 作者: 佚名    | 來源: 網絡轉載     | 查看: 41次 | 進入軟件測試論壇討論

    領測軟件測試網 classIniFile.cls的內容:

    Option Explicit

    '--------classIniFile.cls  代碼----------------
    '這里定義了一個classIniFile類
    '一個絕對經典的在VB中操作.ini文件的通用類源代碼
    '程序編寫:中國青島·許家國
    '    2002.6.16
    'E-Mail: goj2000@163.com
    'HomePage: http://www.gojclub.com
    '

    'Private  member  that  holds  a  reference  to
    'the  path  of  our  ini  file

    Private strINI As String

    'Windows  API  Declares
    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 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 Function MakePath(ByVal strDrv As String, ByVal strDir As String) As String

        '  Makes  an  INI  file:  Guarantees  a  sub  dir
        Do While Right$(strDrv, 1) = "\"
              strDrv = Left$(strDrv, Len(strDrv) - 1)
        Loop
        
        Do While Left$(strDir, 1) = "\"
              strDir = Mid$(strDir, 2)
        Loop
        
        '  Return  the  path
        MakePath = strDrv & "\" & strDir
    End Function

    Private Sub CreateIni(strDrv As String, strDir As String)
        '  Make  a  new  ini  file
        strINI = MakePath(strDrv, strDir)
    End Sub

    Public Sub WriteIniKey(strSection As String, strKey As String, strValue As String)
        '  Write  to  strINI
        WritePrivateProfileString strSection, strKey, strValue, strINI
    End Sub

    Public Function GetIniKey(strSection As String, strKey As String) As String
        Dim strTmp As String
        Dim lngRet As String
        Dim I As Integer
        Dim strTmp2 As String
        
        strTmp = String$(1024, Chr(32))
        lngRet = GetPrivateProfileString(strSection, strKey, "", strTmp, Len(strTmp), strINI)
        strTmp = Trim(strTmp)
        strTmp2 = ""
        For I = 1 To Len(strTmp)
            If Asc(Mid(strTmp, I, 1)) <> 0 Then
                strTmp2 = strTmp2 + Mid(strTmp, I, 1)
            End If
        Next I
        strTmp = strTmp2
        
        GetIniKey = strTmp
    End Function

    Public Property Let INIFileName(ByVal New_IniPath As String)
        '  Sets  the  new  ini  path
        strINI = New_IniPath
    End Property

    Public Property Get INIFileName() As String
        '  Returns  the  current  ini  path
        INIFileName = strINI
    End Property

    '***************************************清除KeyWord"鍵"(Sub)***********************************************
    Public Function DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
        Dim RetVal As Integer
        RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)
    End Function

    '如果是清除section就少寫一個Key多一個""。
    '**************************************清除 Section"段"(Sub)***********************************************
    Public Function DelIniSec(ByVal SectionName As String)      '清除section
        Dim RetVal As Integer
        RetVal = WritePrivateProfileString(SectionName, 0&, "", strINI)
    End Function



    Form1中的內容:

    Option Explicit

    '一個絕對經典的在VB中操作.ini文件的通用類源代碼示例程序
    '程序編寫:中國青島·許家國
    '    2002.6.16
    'E-Mail: goj2000@163.com
    'HomePage: http://www.gojclub.com

    '定義一個.ini類型的變量
    Dim DemoIni As New classIniFile

    Private Sub Form_Load()
        '對控件進行初始化
        Text1.Text = "測試一下"
        List1.Clear
        
        '定義.ini文件名,并寫入一些初始數據
        DemoIni.INIFileName = App.Path & "\demoini.ini"
        DemoIni.WriteIniKey "系統", "啟動路徑", App.Path
        DemoIni.WriteIniKey "系統", "可執行程序文件名", App.EXEName
        
        '顯示保存到.ini文件中的數據
        Call CmdRead_Click
    End Sub

    '退出程序
    Private Sub CmdExit_Click()
        Unload Me
    End Sub

    '讀取.ini文件中已經存在的數據并顯示出來
    Private Sub CmdRead_Click()
        Dim TestStr As String
        
        List1.Clear
        TestStr = DemoIni.GetIniKey("系統", "啟動路徑")
        List1.AddItem "系統 - 啟動路徑: " & TestStr
        TestStr = DemoIni.GetIniKey("系統", "可執行程序文件名")
        List1.AddItem "系統 - 可執行程序文件名: " & TestStr
        TestStr = DemoIni.GetIniKey("用戶自定義", "用戶數據")
        List1.AddItem "用戶自定義 - 用戶數據: " & TestStr
    End Sub

    '保存用戶自定義數據到.ini文件中
    Private Sub CmdSave_Click()
        DemoIni.WriteIniKey "用戶自定義", "用戶數據", Text1.Text
        
        '顯示保存到.ini文件中的數據
        Call CmdRead_Click
    End Sub

    '清除用戶自定義段和段中數據
    Private Sub CmdDelete_Click()
        DemoIni.DelIniKey "用戶自定義", "用戶數據"
        DemoIni.DelIniSec "用戶自定義"
        
        '顯示保存到.ini文件中的數據
        Call CmdRead_Click
    End Sub

    延伸閱讀

    文章來源于領測軟件測試網 http://www.kjueaiud.com/


    關于領測軟件測試網 | 領測軟件測試網合作伙伴 | 廣告服務 | 投稿指南 | 聯系我們 | 網站地圖 | 友情鏈接
    版權所有(C) 2003-2010 TestAge(領測軟件測試網)|領測國際科技(北京)有限公司|軟件測試工程師培訓網 All Rights Reserved
    北京市海淀區中關村南大街9號北京理工科技大廈1402室 京ICP備10010545號-5
    技術支持和業務聯系:info@testage.com.cn 電話:010-51297073

    軟件測試 | 領測國際ISTQBISTQB官網TMMiTMMi認證國際軟件測試工程師認證領測軟件測試網

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>