请问如何遍历ini文件中的节和键值?

panyong751118 2006-11-13 04:06:15
如题
...全文
756 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
KahnWinsock 2006-11-18
  • 打赏
  • 举报
回复
To panyong751118 (今天你抵日了没有?)
.net里声明API时,声明的方式需要注意,尤其是使用到全角字符的时候,你用到的GetPrivateProfileString声明如下

<DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer
End Function
feeling3 2006-11-17
  • 打赏
  • 举报
回复
建议用 XML 了哈
HAVENT 2006-11-16
  • 打赏
  • 举报
回复
以下是INI配置文件读取方法,遍历就自己搞定吧

'声明INI配置文件读写API函数
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32

'定义读取配置文件函数
Public Function GetINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As String
Dim Str As String = LSet(Str, 256)
GetPrivateProfileString(Section, AppName, lpDefault, Str, Len(Str), FileName)
Return Microsoft.VisualBasic.Left(Str, InStr(Str, Chr(0)) - 1)
End Function

'定义写入配置文件函数
Public Function WriteINI(ByVal Section As String, ByVal AppName As String, ByVal lpDefault As String, ByVal FileName As String) As Long
WriteINI = WritePrivateProfileString(Section, AppName, lpDefault, FileName)
End Function
Vb_Net4Zeng 2006-11-15
  • 打赏
  • 举报
回复
用正则表达式吧,在复杂的结构都容易处理
水如烟 2006-11-13
  • 打赏
  • 举报
回复
其实读取固定格式的文本是很简单的,比如:

KeyValue.txt文件:
[Root1]
Key1=0
Key2=中国
Key3=Bool
Key4=-1

[Root2]
Key1=人民
Key2=中国
Key3=Bool
Key4=-1

[Root3]
Key1=0
Key2=中国

[Root4]
Key4=-1

做个类,简单的:
Public Class KeyValue
Public KeyValueCollection As New Collections.Generic.Dictionary(Of String, System.Collections.Generic.Dictionary(Of String, Object))

Public Sub Read(ByVal txtFile As String)
Dim mCurrentRoot As String = Nothing

For Each line As String In System.IO.File.ReadAllLines(txtFile, System.Text.Encoding.Default)
If line.StartsWith("[") AndAlso line.EndsWith("]") Then
mCurrentRoot = line.Substring(1, line.Length - 2)
Me.KeyValueCollection.Add(mCurrentRoot, New System.Collections.Generic.Dictionary(Of String, Object))
Else
Dim mArray() As String = line.Split("=")
If mArray.Length = 2 Then
Me.KeyValueCollection(mCurrentRoot).Add(mArray(0), mArray(1))
End If
End If
Next
End Sub

Public Sub PrintOut()
For Each root As String In Me.KeyValueCollection.Keys
Console.WriteLine()
Console.WriteLine("[{0}]", root)
For Each Key As String In Me.KeyValueCollection(root).Keys
Console.WriteLine("{0} = {1}", Key, Me.KeyValueCollection(root)(Key))
Next
Next

End Sub

End Class

使用:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim t As New KeyValue
t.Read("KeyValue.txt")
t.PrintOut()
End Sub
输出结果:

[Root1]
Key1 = 0
Key2 = 中国
Key3 = Bool
Key4 = -1

[Root2]
Key1 = 人民
Key2 = 中国
Key3 = Bool
Key4 = -1

[Root3]
Key1 = 0
Key2 = 中国

[Root4]
Key4 = -1
panyong751118 2006-11-13
  • 打赏
  • 举报
回复
得到节点基本搞定,但是碰到新问题,如果键值是中文的话,通过GetPrivateProfileString得到的是乱码!怎么搞?
peilianhai 2006-11-13
  • 打赏
  • 举报
回复
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
'返回一个字符串数组
'用来读string1,string2....



'获得指定ini文件中某个节下面某个子键的键值,需要下面的API声明
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
用来读color,Text...


panyong751118 2006-11-13
  • 打赏
  • 举报
回复
我的ini是如下结构:
[String1]
Color=...
Text=...
...
[String2]
Color=...
Text=...
...
[StringN]
Color=...
Text=...
...
但是由于N是不确定的,所以需要遍历把所有的“节”及键值都取出来。
peilianhai能说的详细点吗?谢谢!
peilianhai 2006-11-13
  • 打赏
  • 举报
回复
遍历,用递归循环
取值,调用api
真相重于对错 2006-11-13
  • 打赏
  • 举报
回复
call
GetProfileString api
ms-help://MS.MSDNQTR.2003FEB.2052/sysinfo/base/getprofilestring.htm

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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