我设法得到一个写入和读取INI文件的类。以下是代码:
Public Class IniFile
' API functions
Private Declare Ansi Function GetPrivateProfileString _
Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, ByVal lpFileName As String) _
As Integer
Private Declare Ansi Function WritePrivateProfileString _
Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpString As String, _
ByVal lpFileName As String) As Integer
Private Declare Ansi Function GetPrivateProfileInt _
Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal nDefault As Integer, _
ByVal lpFileName As String) As Integer
Private Declare Ansi Function FlushPrivateProfileString _
Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As Integer, _
ByVal lpKeyName As Integer, ByVal lpString As Integer, _
ByVal lpFileName As String) As Integer
Dim strFilename As String
' Constructor, accepting a filename
Public Sub New(ByVal Filename As String)
strFilename = Filename
End Sub
' Read-only filename property
ReadOnly Property FileName() As String
Get
Return strFilename
End Get
End Property
Public Function GetString(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As String) As String
' Returns a string from your INI file
Dim intCharCount As Integer
Dim objResult As New System.Text.StringBuilder(256)
intCharCount = GetPrivateProfileString(Section, Key, _
[Default], objResult, objResult.Capacity, strFilename)
If intCharCount > 0 Then GetString = _
Left(objResult.ToString, intCharCount)
End Function
Public Function GetInteger(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As Integer) As Integer
' Returns an integer from your INI file
Return GetPrivateProfileInt(Section, Key, _
[Default], strFilename)
End Function
Public Function GetBoolean(ByVal Section As String, _
ByVal Key As String, ByVal [Default] As Boolean) As Boolean
' Returns a boolean from your INI file
Return (GetPrivateProfileInt(Section, Key, _
CInt([Default]), strFilename) = 1)
End Function
Public Sub WriteString(ByVal Section As String, _
ByVal Key As String, ByVal Value As String)
' Writes a string to your INI file
WritePrivateProfileString(Section, Key, Value, strFilename)
Flush()
End Sub
Public Sub WriteInteger(ByVal Section As String, _
ByVal Key As String, ByVal Value As Integer)
' Writes an integer to your INI file
WriteString(Section, Key, CStr(Value))
Flush()
End Sub
Public Sub WriteBoolean(ByVal Section As String, _
ByVal Key As String, ByVal Value As Boolean)
' Writes a boolean to your INI file
WriteString(Section, Key, CStr(CInt(Value)))
Flush()
End Sub
Private Sub Flush()
' Stores all the cached changes to your INI file
FlushPrivateProfileString(0, 0, 0, strFilename)
End Sub
End Class
我用它来写一个INI文件。输出是:
[AF23AE0F]
Title=Title
Username=Username
[4C347E51]
Title=Title
Username=Username
该部分包含分配给密钥的特定ID。例如一旦我输入“AF23AE0F”作为部分,它将获得与“AF23AE0F”部分相关的键。由于我有多个部分,我想阅读每个部分,并在每个部分下获取密钥并将它们添加到Listview。
ListView1.Items.Add(New ListViewItem(New String() {ID, Title, Username}))
上面是我用来添加到listview的代码,这个方法为每个列添加了项目。 为了获得我使用的部分:
Dim objIniFile As New IniFile("C:\data.ini")
objIniFile.GetString("Get Each Section ID", "Title", "Get Value related to 'Title'")
通过For Each
语句,我希望获得每个部分并读取ID并将其添加到上面的方法中,结果将读取每个键。
如何阅读每个部分并获取密钥并将其添加到列表视图中?
答案 0 :(得分:1)
好吧,既然您实际上并没有被迫使用.ini文件,为什么不及时进行20年的飞跃并使用XML序列化来读取和写入结构化文件?< / p>
Module Module1
Private Sub WriteUsers(Filename As String)
'Create an example user list
Dim Users As New List(Of User)
Users.Add(New User With {.ID = "JAOIJSFL", .Title = "Hello", .Username = "Guy 1"})
Users.Add(New User With {.ID = "LKGJASOI", .Title = "World", .Username = "Guy 2"})
'Initialize a new XmlSerializer with the type of data you want to write
Dim Serializer As New System.Xml.Serialization.XmlSerializer(GetType(List(Of User)))
'Create the file and write the list to it
Using fs As New IO.FileStream(Filename, IO.FileMode.Create)
Serializer.Serialize(fs, Users)
End Using
End Sub
Private Function ReadUsers(Filename As String) As List(Of User)
'Initialize a new XmlSerializer with the type of data you want to read
Dim Serializer As New System.Xml.Serialization.XmlSerializer(GetType(List(Of User)))
'Open the input file, read and return the data
Using fs As New IO.FileStream(Filename, IO.FileMode.Open)
Return CType(Serializer.Deserialize(fs), List(Of User))
End Using
End Function
Sub Main()
'Write data
WriteUsers("C:\test\test.xml")
'Read the written data back
Dim users As List(Of User) = ReadUsers("C:\test\test.xml")
'Output the results
For Each u As User In users
Console.WriteLine(String.Format("ID: {0}, Title: {1}, Name: {2}", u.ID, u.Title, u.Username))
Next
Console.ReadKey()
End Sub
End Module
Public Class User
'This class contains all the information about each 'section' that you need
Public Property ID As String
Public Property Username As String
Public Property Title As String
End Class
这使用自动生成XML文档来将包含您的信息的自定义类的列表写入文件。然后,它可以直接从文件中再次将此列表再现到一个方便的列表中。与ini文件相比,它有很多优点。