Search code examples
vb.netdirectorylistboxdirectory-structure

List files in list box with no sub folder in VB.NET


I see everyone iis trying to add their sub folders in their code. I am trying to get mine out. I have a folder I need to read from but not the sub folders within in it. I am also trying to read from two different folders into one list box. Currently in my program I have two list box's and 3 different locations. I am needing to combine two of the folders into one listbox and as I said keep the sub folders out.

HourlyList.SelectionMode = SelectionMode.MultiSimple
HourlyList.Items.Clear()
Dim directory = "C:\filepath\filepathFolder"
Dim dirinfo AS New System.IO.DirectoryInfo(directory)
list = dirinfo.GetFiles(2DArray(Row,Column) + "*", IO.SearchOption.AllDirectories)
For Each file In list
    HourlyList.Items.Add(file) 

Solution

  • Why don't you use SearchOption.TopDirectoryOnly if you don't want the sub-folders? You can use EnumerateFiles and Concat to get all files in both folders:

    Dim filesFolder1 = directory1.EnumerateFiles("*", SearchOption.TopDirectoryOnly)
    Dim filesFolder2 = directory2.EnumerateFiles("*", SearchOption.TopDirectoryOnly)
    
    For Each file In filesFolder1.Concat(filesFolder2)
        HourlyList.Items.Add(file)
    Next