‘代码为VBNET

‘例子:

‘xx.ini文件内容如下:

‘[广东]
‘gz=广州
‘hz=惠州
‘[福建]
‘fz=福州
‘xm=厦门

‘代码:

‘dd = New iniCLass.zjIniClass(Application.StartupPath & “\xx.ini”)

‘Mmsgbox( dd.info.jSonO.item(“广东”).item(“gz”))

 

‘—————————————————

‘类代码,指定了命名空间为iniClass

Imports System.Text.RegularExpressions
Public Class zjIniClass

Private iniFileUrl As String
Public info As JSON
Private iniString As String

Public Sub New(ByVal inifile As String)
iniFileUrl = inifile
ini()
End Sub

Public Sub ini()
Dim reader As System.IO.StreamReader
Dim lineTxt As String
Dim secR As New Regex("^\[[^\]]*\]$")
Dim protoR As New Regex("^[^=]*=\S*\s*")
Dim i As Integer
i = 0
iniString = "{"
reader = New System.IO.StreamReader(iniFileUrl, System.Text.Encoding.Default, False)

Do While True
lineTxt = reader.ReadLine
If lineTxt Is Nothing Then Exit Do
i = i + 1
If secR.IsMatch(lineTxt) Then
If i > 1 Then
iniString = Left(iniString, Len(iniString) - 1) & "},"
End If
iniString = iniString & """" & Replace(Replace(lineTxt, "[", ""), "]", "") & """" & ":{"
ElseIf protoR.IsMatch(lineTxt) Then
iniString = iniString & """" & Mid(lineTxt, 1, InStr(lineTxt, "=") - 1) & """:""" & Mid(lineTxt, InStr(lineTxt, "=") + 1) & ""","
End If

Loop
iniString = Left(iniString, Len(iniString) - 1)
iniString = iniString & "}}"
info = New JSON(iniString)
End Sub

Public Sub save()
End Sub

End Class