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
'声明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
做个类,简单的:
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
输出结果:
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...