VB如何指定读取一个INI文件的某个数据?

Wellmon 2014-02-08 05:12:18
例如下面是一个名为config.ini的配置文件,我需要让VB读取homePage等号后面的数据到窗口中Label1控件的Caption?还有怎么保存Label1的数据到程序根目录下config.ini的homePage值?

[config]
homePage=http://www.baidu.com/
...全文
379 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Wellmon 2014-03-03
  • 打赏
  • 举报
回复
两个回答都不错,赞一个!
michaelbob518 2014-02-27
  • 打赏
  • 举报
回复
怎么还不结贴呢。。。。
michaelbob518 2014-02-12
  • 打赏
  • 举报
回复
建一个类模块..代码

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 File As String

Public Sub DeleteSection(ByVal Section As String) '删除节的过程

    Dim ret As Integer

    ret = WritePrivateProfileString(Section, 0&, "", File)
End Sub

Public Function SaveSetting(ByVal Section$, ByVal Key$, ByVal Value$)
    '保存数据的函数
    SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File)
End Function

Public Function GetSetting(ByVal Section As String, _
                           ByVal KeyName As String) As String '取得数据的函数

    Dim ret As Integer

    Dim t   As String * 1000

    ret = GetPrivateProfileString(Section, KeyName, "unknown value", t, Len(t), File) '注意默认值为"unknown value",也可设为""

    If ret > 0 Then
        GetSetting = Left$(t, ret)
    Else
        GetSetting = "Unknown section or key" '出错则返回"Unknown section or key"
    End If

End Function

Public Property Get filename() As String
    filename = File
End Property

Public Property Let filename(ByVal NewFileName As String)
    File = NewFileName
End Property

然后下面是调用示例

 IniUtil.GetSetting("config", "homePage") '这个是读取,
IniUtil.SaveSetting("config", "homePage" ,"保存值") '这个保存
iniutil是实例名,需要new出来后的.
of123 2014-02-08
  • 打赏
  • 举报
回复
代码中的 Settings 是 ini 文件中用方括号括起来的应用名。这个根据你的情况去改。
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 Sub Form_Load()
Dim tmp As String

    tmp = Space(255)
    GetPrivateProfileString "Settings", "homePage", "", tmp, 255, App.Path & "\config.ini"
    Label1 = tmp
End Sub

Private Sub Form_Unload(Cancel As Integer)
    'Save settings
    WritePrivateProfileString "Settings", "homePage", Label1.Caption, App.Path & "\config..ini"
End Sub

7,759

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧