浅谈vb中的ini文件保存数据功能

    文章来源:万象互联 更新时间:2012-11-5 16:53:43
分享:

首先,先新建一个类模块:classIniFile

Option Explicit

Private strINI As String

'Windows API 声明

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)          '写ini主键
    ' Write to strINI
    WritePrivateProfileString strSection, strKey, strValue, strINI
End Sub

Public Function GetIniKey(strSection As String, strKey As String) As String            '读ini主键
    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 Sub DelIniKey(ByVal SectionName As String, ByVal KeyWord As String)
    Dim RetVal As Integer
    RetVal = WritePrivateProfileString(SectionName, KeyWord, 0&, strINI)
End Sub

'如果是清除section就少写一个Key多一个""。

Public Sub DelIniSec(ByVal SectionName As String)      '清除section
    Dim RetVal As Integer
    RetVal = WritePrivateProfileString(SectionName, 0&, "", strINI)
End Sub
'类模块代码结束

**********************************************************************************
新建窗体form1:(加一个text1, 加一个listview, 四个按钮,)

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.hulian.top,转载请注明!

版权说明:本站原创文章,由万象互联SEO优化发表.
本文地址:https://www.hulian.top/zixun/post/5368.html
在线咨询
  • 在线时间
  • 8:00-21:00