急...在线等!ini文件如何在不知道有多少个key的情况下,读取某小节中的全部内容.
现在的状况:
不知道key的值 也不知道有多少个key
请教如何读出一个项目下所有的值
看了csdn上一些有关的帖子了 都太复杂 只好留着以后仔细研究
因为现在只急需实现这个单一功能
急!!!! 在线等
谢谢
[解决办法]
Option Explicit'INI文件读写类(未完成)''与普通INI读写功能类似,增加了节与关键字集合,读入到一个数组内,方便枚举.'目前只有枚举,读值功能,还没增加写值,新建啥的功能....因为用到这个功能的那个工程不需要写,就懒得写了,哇哈哈..-_-b''BY 嗷嗷叫的老马'http://www.m5home.comDim mIniFileName As String 'INI文件名Dim IniBuff() As String '保存INI内容的数组Public Function EnumSections() As String() '取得所有的节名称 Dim Buff() As String, I As Long, J As Long If UBound(IniBuff) < 0 Then Exit Function J = 0 For I = 0 To UBound(IniBuff) If Mid(IniBuff(I), 1, 1) = "[" Then ReDim Preserve Buff(J) '记录当前关键字 Buff(J) = IniBuff(I) J = J + 1 End If Next EnumSections = BuffEnd FunctionPublic Function EnumKeywords(ByVal SectionName As String) As String() '根据节名称,返回所有关键字 Dim Buff() As String, I As Long, J As Long, K As Long If UBound(IniBuff) < 0 Then Exit Function J = 0 For I = 0 To UBound(IniBuff) If Mid(IniBuff(I), 1, 1) = "[" And InStr(IniBuff(I), SectionName) <> 0 Then '从包含"["且包含节名的行开始 Do I = I + 1 '下一行 If I > UBound(IniBuff) Then Exit Do If Mid(IniBuff(I), 1, 1) = "[" Then Exit Do '如果包含"[",说明到达下一个节开始了,跳出 If IniBuff(I) <> "" Then ReDim Preserve Buff(J) '记录当前关键字 Buff(J) = IniBuff(I) J = J + 1 End If Loop Exit For End If Next EnumKeywords = BuffEnd FunctionPublic Function GetValue(ByVal SectionName As String, ByVal Keyword As String) As String '根据节名与关键字,返回值 Dim Buff() As String, I As Long, J As Long, K As Long On Error GoTo ErrHandle If UBound(IniBuff) < 0 Then Exit Function J = 0 For I = 0 To UBound(IniBuff) If Mid(IniBuff(I), 1, 1) = "[" And InStr(IniBuff(I), SectionName) <> 0 Then '从包含"["且包含节名的行开始 Do I = I + 1 '下一行 If Mid(IniBuff(I), 1, 1) = "[" Then Exit Do '如果包含"[",说明到达下一个节开始了,跳出 If InStr(IniBuff(I), Keyword) <> 0 Then Buff = Split(IniBuff(I), "=") If Buff(0) = Keyword Then '确认关键字相同,而不是包含 GetValue = Buff(1) '记录当前关键字 Exit Do End If End If J = J + 1 Loop Exit For End If NextErrHandle:End FunctionPublic Property Get IniFileName() As String IniFileName = mIniFileNameEnd PropertyPublic Property Let IniFileName(ByVal vNewValue As String) mIniFileName = vNewValue Open mIniFileName For Binary As #1 Dim Buff As String Buff = Space(LOF(1)) Get #1, , Buff Close #1 IniBuff = Split(Buff, vbCrLf)End Property
[解决办法]
支持老马~~~~~~~~~~~~
[解决办法]
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 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
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
' <CSCM>
'--------------------------------------------
' 工程 :
' 过程 : GetAllSection
' 描述 : [取得指定关键字下的所有值 ,strSection不区分大小写]
' 创建人 : Project Administrator aohan
' 计算机 :
' 日期时间 : 2008-12-15-08:44:41
'
' 参数 : strSection (String) As String()
'--------------------------------------------
' </CSCM>
Public Function GetAllSection(strSection As String) As String()
Dim strReturn As String * 32767 'windows系统中最大值为32767
Dim strTmp As String
Dim nStart As Integer
Dim nEnd As Integer
Dim i As Integer
Dim sArray() As String
Call GetPrivateProfileSection(strSection, strReturn, Len(strReturn), strINI)
strTmp = strReturn
i = 1
Do While strTmp <> ""
nStart = nEnd + 1
nEnd = InStr(nStart, strReturn, vbNullChar)
strTmp = Mid$(strReturn, nStart, nEnd - nStart)
If Len(strTmp) > 0 Then
ReDim Preserve sArray(1 To i)
sArray(i) = strTmp
i = i + 1
End If
Loop
GetAllSection = sArray
End Function
Public Property Let INIFileName(ByVal New_IniPath As String)
' 设置新的Ini文件路径
strINI = New_IniPath
End Property
Public Property Get INIFileName() As String
' 返回当前的Ini文件路径
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
Private Sub Class_Initialize()
'根据需要可以调整这个初始化的值
strINI = App.Path & "SysSet.ini" '类初始化时,默认指定配置文件名为当前文件下的SysSet.Ini文件,方便操作
End Sub
'调用应该会吧,你需要的功能就是GetAllSection方法返回的数组
Private Sub Command1_Click() Open "c:\setup.ini" For Input As #1 Dim tmp As String Do While Not EOF(1) Line Input #1, tmp If tmp = "[显示统计线]" Then Exit Do'小节名写在方括号里 Loop Do While Not EOF(1) Line Input #1, tmp If InStr(tmp, "[") = 0 And tmp <> "" Then Debug.Print Split(tmp, "=")(0), Split(tmp, "=")(1) Else Close Exit Do End If Loop Close End Sub
[解决办法]
纯粹字符函数处理即可,先截取出这一段的整个内容,然后每行处理,可以配合正则使用
[解决办法]