GetDirectory will not list all Directories

120 views Asked by At

I have a form that allows you to click a button, which triggers an OpenFileDialog. From there, you are suppose to select a specific file within that folder, and then the program is supposed to go through from the folder you were in the the /subjects folder and list those directories.

At the moment, I have 3 directories within /subjects: english, mathematics, and cte.

My issue is that when the program is ran, it will only list the English directory in the combo-box, and will not list any of the others.

    Private Sub btnDocumentChoice_Click(sender As Object, e As EventArgs) Handles btnDocumentChoice.Click
    Dim ofd As New OpenFileDialog
    Dim DirList As New ArrayList
    If ofd.ShowDialog = Windows.Forms.DialogResult.OK AndAlso ofd.FileName <> "" Then
        strRootLocation = (Path.GetDirectoryName(ofd.FileName))
        GetDirectories(strRootLocation + "/subject/", DirList)
        'MessageBox.Show(Path.GetDirectoryName(ofd.FileName))
    End If
End Sub


Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    strRootLocation = OpenFileDialog1.FileName
    cmbSubject.Items.Add(strRootLocation)
End Sub

Sub GetDirectories(ByVal StartPath As String, ByRef DirectoryList As ArrayList)
    Dim Dirs() As String = Directory.GetDirectories(StartPath)
    DirectoryList.AddRange(Dirs)
    For Each Dir As String In Dirs
        GetDirectories(Dir, DirectoryList)
        cmbSubject.Items.Add(Replace(Path.GetDirectoryName(Dir), strRootLocation + "\subject", ""))
        cmbSubject.Items.Remove("")
    Next
End Sub
1

There are 1 answers

0
Blake Stoddard On BEST ANSWER

I managed to fix my own issue by removing the For Each loop in the question, and replacing it with this:

        Dim directories As String
    For Each directories In Directory.GetDirectories(strRootLocation + "\subject")
        cmbSubject.Items.Add(Replace(directories, strRootLocation + "\subject\", ""))
    Next