在我的程序中基本上有表单A和表单B.在表单A中,用户添加某些信息,当他点击按钮时,会创建一个具有唯一ID的文件夹,并在该文件夹中创建一个文本文件。每当用户想要保存更多信息时,每次执行此操作时,都会创建一个文件夹并将文本文件保存在其中。
在我的表格B中,我添加了一个ListView并将样式设置为详细信息。我想要做的是每次加载表单时,程序将从例如保存的目录中读取每个文本文件。 C:/并添加读取的项目。我的listView包含以下列:Username, Description
,因此,我要将项目填充到相关列中,我会执行以下操作:
ListView1.Items.Add(New ListViewItem({"Username", "Description"}))
如何创建它以便从创建的文件夹中读取每个文本文件并将Listview填充到我想要的列中?
答案 0 :(得分:1)
我设法通过添加循环解决了这个问题。
Public Sub ReadFile()
For Each Dir As String In Directory.GetDirectories(GetFolderPath(SpecialFolder.ApplicationData) + "\Folder\Sub-Folder\")
Dim file1 As String() = Directory.GetFiles(Dir, "file1")
Dim file2 As String() = Directory.GetFiles(Dir, "file2")
Dim file3 As String() = Directory.GetFiles(Dir, "file3")
Dim file4 As String() = Directory.GetFiles(Dir, "file4")
Dim Finalfile1 As String
Dim Finalfile2 As String
Dim Finalfile3 As String
Dim Finalfile4 As String
For Each Finalfile1 In file1
Using readfile1 As StreamReader = New StreamReader(Finalfile1)
Dim line As String = readfile1.ReadLine
End Using
Next
For Each Finalfile2 In file2
Using readfile2 As StreamReader = New StreamReader(Finalfile2)
Dim line1 As String = readfile2.ReadLine
End Using
Next
For Each Finalfile3 In file3
Using readfile3 As StreamReader = New StreamReader(Finalfile3)
Dim line2 As String = readfile3.ReadLine
End Using
Next
For Each Finalfile4 In file4
Using readfile4 As StreamReader = New StreamReader(Finalfile4)
Dim line3 As String = readfile4.ReadLine
End Using
Next
ListView1.Items.Add(New ListViewItem({addName, addUsername, addWebsite, addDescription}))
Next
End Sub
这样做是因为我添加了一个找到某个文件名的字符串。然后将从每个文件夹中读取,然后将其添加到相关列。