none
VB List Files in Directory RRS feed

  • Question

  • I'm trying to list all the files in a specified directory into a list box. From what I've read, I'm supposed to use the following:

    For Each foundFile In My.Computer.FileSystem.GetFiles(
            My.Computer.FileSystem.SpecialDirectories.MyDocuments)
        ListBox1.Items.Add(foundFile)
    Next

    but with this I'm not sure how to specify the folder I want to list. I tried to run it as shown, thinking it might give me a map of my whole system, but instead got an error. What am I not seeing?

    Friday, September 27, 2019 2:43 PM

Answers

  • Hi,

    Do you want this effect? Show all the system directories, select a specific directory, and then display all the files in the directory on the ListBox.

    If so, you can try my code as follows.

    You can also enter a specific directory in the TextBox then display it.

    Imports System.IO
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Give you a map of your whole system
            Dim selectedPath As String = GetSelectedPath()
            If "".Equals(selectedPath) Then
                MsgBox("Please select lagal path!")
                GetSelectedPath()
            End If
            ListBox1.Items.Clear()
            Lookfile(selectedPath)
    
            ''input a specified directory just like D: or D:\
            'Dim pathname As String
            'pathname = TextBox1.Text
            'Lookfile(pathname)
    
        End Sub
    
        Sub Lookfile(pathname As String)
            If pathname.Trim().Length = 0 Then
                Return
            End If
            Dim files As String() = Directory.GetFileSystemEntries(pathname)
            Try
                For Each dir As String In files
    
                    If Directory.Exists(dir) Then
                        Lookfile(dir)
                    Else
                        ListBox1.Items.Add(dir)
                    End If
                Next
            Catch ex As Exception
                ex.ToString()
            End Try
        End Sub
        Private Function GetSelectedPath() As String
    
            Dim fbd As FolderBrowserDialog = New FolderBrowserDialog()
    
            If fbd.ShowDialog() = DialogResult.OK Then
                Return fbd.SelectedPath
            End If
            Return ""
    
        End Function
    End Class
    

    Hope I can Help you.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 30, 2019 7:15 AM
    Moderator

All replies

  • Hi

    Here, the following works fine. The only change I needed to make due to my use of Option Strict On was to add a qualifier type for the loop ('As String') - which I would suppose you don't need?

    I also added the second parameter in the call to GetFiles of  ', FileIO.SearchOption.SearchAllSubDirectories' to add the files contained in the sub folders.

    If you want, you can easily add a FolderBrowser dialog and let the User choose which folder to List. There are so many variations that can be implemented.

    Anyway, here is the code I tested with:

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		For Each foundFile As String In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyDocuments, FileIO.SearchOption.SearchAllSubDirectories)
    			ListBox1.Items.Add(foundFile)
    		Next
    	End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Friday, September 27, 2019 3:19 PM
    Friday, September 27, 2019 3:17 PM
  • Hi,

    Do you want this effect? Show all the system directories, select a specific directory, and then display all the files in the directory on the ListBox.

    If so, you can try my code as follows.

    You can also enter a specific directory in the TextBox then display it.

    Imports System.IO
    
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'Give you a map of your whole system
            Dim selectedPath As String = GetSelectedPath()
            If "".Equals(selectedPath) Then
                MsgBox("Please select lagal path!")
                GetSelectedPath()
            End If
            ListBox1.Items.Clear()
            Lookfile(selectedPath)
    
            ''input a specified directory just like D: or D:\
            'Dim pathname As String
            'pathname = TextBox1.Text
            'Lookfile(pathname)
    
        End Sub
    
        Sub Lookfile(pathname As String)
            If pathname.Trim().Length = 0 Then
                Return
            End If
            Dim files As String() = Directory.GetFileSystemEntries(pathname)
            Try
                For Each dir As String In files
    
                    If Directory.Exists(dir) Then
                        Lookfile(dir)
                    Else
                        ListBox1.Items.Add(dir)
                    End If
                Next
            Catch ex As Exception
                ex.ToString()
            End Try
        End Sub
        Private Function GetSelectedPath() As String
    
            Dim fbd As FolderBrowserDialog = New FolderBrowserDialog()
    
            If fbd.ShowDialog() = DialogResult.OK Then
                Return fbd.SelectedPath
            End If
            Return ""
    
        End Function
    End Class
    

    Hope I can Help you.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 30, 2019 7:15 AM
    Moderator
  • Thanks Les. I got called away to some other work. Hopefully I'll have a chance to test this soon.
    Monday, September 30, 2019 1:33 PM
  • Thanks Julie. I got called away to some other work. This is pretty much what I'm looking for. Hopefully I'll get a chance to test it soon.
    Monday, September 30, 2019 1:34 PM
  • Hi,

    Do you resolve the issue? If you resolve the issue, please mark the helpful as answer. It will be beneficial to other community.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 2, 2019 1:55 AM
    Moderator
  • Hi Julie, Thanks for the solution above. Requirements have changed. Now I need to write to a ListView control and include folders as well as files. Output would be like:

    FolderA  (this would be the folder the user selected)

    FileA1

    FolderB

    FolderB1

    FileB1

    File B11

    etc.

    Any suggestions?

    Thursday, October 3, 2019 11:39 AM