找回密码
 注册

QQ登录

只需一步,快速开始

智能终端设备维修查询系统注册会员邮箱认证须知!
查看: 6964|回复: 2

[VB例程源码] VB中操作.ini文件的通用类源码

[复制链接]
  • TA的每日心情

    2025-6-23 21:25
  • 签到天数: 126 天

    [LV.7]常住居民III

    admin 发表于 2009-12-18 00:49:08 | 显示全部楼层 |阅读模式

    欢迎您注册加入!这里有您将更精采!

    您需要 登录 才可以下载或查看,没有账号?注册

    ×
    classIniFile.cls的内容:
    [mw_shl_code=vb,true]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[/mw_shl_code]


    该用户从未签到

    xueming 发表于 2010-4-10 17:27:27 | 显示全部楼层
    没怎么看懂,知识还是要持之以恒
    回复

    使用道具 举报

  • TA的每日心情

    2012-4-18 06:53
  • 签到天数: 1 天

    [LV.1]初来乍到

    nwfoaf 发表于 2012-1-18 00:10:57 | 显示全部楼层
    有意思的代码
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    免责声明

    本站中所有被研究的素材与信息全部来源于互联网,版权争议与本站无关。本站所发布的任何软件编程开发或软件的逆向分析文章、逆向分析视频、补丁、注册机和注册信息,仅限用于学习和研究软件安全的目的。全体用户必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。学习编程开发技术或逆向分析技术是为了更好的完善软件可能存在的不安全因素,提升软件安全意识。所以您如果喜欢某程序,请购买注册正版软件,获得正版优质服务!不得将上述内容私自传播、销售或者用于商业用途!否则,一切后果请用户自负!

    QQ|Archiver|手机版|小黑屋|联系我们|宝峰科技 ( 滇ICP备09007156号-2|53050202000040 )

    Copyright © 2001-2023 Discuz! Team. GMT+8, 2025-7-12 15:34 , Processed in 0.097948 second(s), 14 queries , File On Powered by Discuz! X3.59© 2001-2025 Discuz! Team.

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