Zer0
Zer0

Reputation: 1022

How do I read each text file from directories

Basically in my program there is Form A and Form B. In Form A, the user adds certain information and when he clicks on a button, a folder with a unique ID is created and a text file is created in that folder. Whenever the user wants to save more information, each time he does so, a folder gets created and the text file gets saved inside it.

In my Form B, I've added a ListView and set the style to details. What I want to do is that every time the form is loaded, the program will read each text file from the directories saved in e.g. C:/ and add the read items. My listView has these columns: Username, Description, So for me to populate items into the relevant column, I'd do something like:

 ListView1.Items.Add(New ListViewItem({"Username", "Description"}))

How can I make it so that it reads each text file from those folders created and populates the Listview into the columns I want?

Upvotes: 0

Views: 505

Answers (1)

Zer0
Zer0

Reputation: 1022

I managed to solve this problem by adding Loops.

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

What this does is that I've added string which find a certain file name. This will then be read from each folder there is and then added to the relevant column.

Upvotes: 1

Related Questions