1

我有一个允许您单击按钮的表单,该按钮会触发 OpenFileDialog。从那里,您假设在该文件夹中选择一个特定文件,然后程序应该从您在该文件夹中的文件夹中遍历/subjects并列出这些目录。

目前,我有 3 个目录/subjectsenglishmathematicscte.

我的问题是,当程序运行时,它只会在组合框中列出英文目录,而不会列出任何其他的。

    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
4

1 回答 1

0

我设法通过删除问题中的 For Each 循环来解决我自己的问题,并将其替换为:

        Dim directories As String
    For Each directories In Directory.GetDirectories(strRootLocation + "\subject")
        cmbSubject.Items.Add(Replace(directories, strRootLocation + "\subject\", ""))
    Next
于 2013-12-20T20:13:38.200 回答