VB里面如何取得INI文件里面 Section的所有Key值

DHC_KULOU 2008-02-26 04:57:57
写入的格式如下

[8888]
2008/02/26 13:53:12=100
2008/02/26 14:30:23=350
2008/02/26 14:43:20=210


前面的Key就是写入时候的系统时间 后面是设置的数值
怎么样能取到[8888]这个Section里面所有的数据(红色部分)
...全文
575 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
DHC_KULOU 2008-02-27
  • 打赏
  • 举报
回复
感谢 2楼 jennyvenus 发的
已经好用了 呵呵
3楼的朋友 也很感谢 虽然 呵呵呵
aohan 2008-02-26
  • 打赏
  • 举报
回复
参考一下这个类,实现了对ini操作的封装

Option Explicit


'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


'定义一个.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


用户 昵称 2008-02-26
  • 打赏
  • 举报
回复
Private Sub Command1_Click() 
Dim ret As String * 32767
Dim x
x = GetPrivateProfileSection("请填入你想绑定的主机IP&MAC", ret, 255, App.Path & "\\config.ini")
MsgBox x
Text2.Text = Replace(Left(ret, InStr(ret, Chr(0) & Chr(0))), Chr(0), vbCrLf)
End Sub


别人的例子
用户 昵称 2008-02-26
  • 打赏
  • 举报
回复
GetPrivateProfileSection

VB声明
Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
说明
获取指定小节所有项名和值的一个列表
返回值
Long,装载到lpReturnedString缓冲区的字符数量。如缓冲区的容量不够大,不能容下所有信息,就返回nSize-2
参数表
参数 类型及说明
lpAppName String,欲获取的小节。注意这个字串不区分大小写
lpReturnedString String,项和值字串的列表。每个字串都由一个NULL字符分隔,最后一个字串后面用两个NULL字符中止
nSize Long,lpReturnedString缓冲区的大小。在windows系统中最大值为32767
lpFileName String,初始化文件的名字。如没有指定完整路径名,windows就在Windows目录中查找文件
注解
参考对GetPrivateProfileInt函数的注解

1,486

社区成员

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

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