Populating ListBox ONLY with .txt files from a directory

Sh_oK

Member
Joined
Jun 25, 2008
Messages
10
Programming Experience
1-3
Hi!
Like I said in the title, I want to populate a listbox with text files from a folder.
I know hot to populate that listbox with all the files from a directory, but i don't know how to filter those files to .txt files only.
My code for populating the listbox is:

VB.NET:
Expand Collapse Copy
ListBox2.Items.Clear()
        For Each fisier As String In My.Computer.FileSystem.GetFiles(System.AppDomain.CurrentDomain.BaseDirectory())
            ListBox2.Items.Add(Path.GetFileNameWithoutExtension(fisier))
        Next

Thanks!
 
try something like this:
VB.NET:
Expand Collapse Copy
        ListBox2.Items.Clear()
        For Each fisier As String In My.Computer.FileSystem.GetFiles(System.AppDomain.CurrentDomain.BaseDirectory(), FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
            ListBox2.Items.Add(Path.GetFileNameWithoutExtension(fisier))
        Next
 
OK, taking this problem further, how can i read the contents of those text files listed in that listbox?
After i select one of the text files listed in that listbox, i want the information contained by it to be shown in a text box or another listbox.
I tried something like:
VB.NET:
Expand Collapse Copy
Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
        Dim fs As New System.IO.StreamReader(System.AppDomain.CurrentDomain.BaseDirectory())
        Dim strFile As String
        While fs.Peek() >= 0
            strFile = fs.ReadLine()
            ListBox1.Items.Add(strFile)
        End While

    End Sub
but it's not working. I know i miss something... :confused:
 
Problem solved ;)
Here is the code if anyone has a similar problem:

VB.NET:
Expand Collapse Copy
Dim sr As New System.IO.StreamReader(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory(), _
                                                              ListBox2.SelectedItem.ToString + ".txt"))
            Dim strFile As String
            While sr.Peek() >= 0
                strFile = sr.ReadLine()
                ListBox1.Items.Add(strFile)
            End While
 
how can i read the contents of those text files listed in that listbox?
VB.NET:
Expand Collapse Copy
Me.ListBox1.DataSource = IO.File.ReadAllLines("c:\file.txt")
 
Back
Top