基本上,我正在尝试使用静态位置的目录列表填充菜单条,然后嵌套其父目录下的任何子目录,然后在文件夹中显示文件
我有基本的
Dim loc As String = "C:\test\"
For Each dirs As String In Directory.GetDirectories(loc)
Dim dirinfo As New DirectoryInfo(dirs)
p1.DropDownItems.Add(dirinfo.Name)
Next
这给了我第一行目录,但不知道从哪里开始。最后,我希望能够在此处打开文件表单。
任何人都可以指出我正确的方向
由于
答案 0 :(得分:1)
这对我有用,虽然它似乎只有四个层次
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' assuming you have a single top level menu item named p1
Dim loc As String = "C:\test\"
AddSubDirsToMenuRecursive(p1, loc)
End Sub
Private Sub AddSubDirsToMenuRecursive(m As ToolStripMenuItem, d As String)
Dim dirs = Directory.GetDirectories(d)
For Each dir As String In dirs
Dim dirinfo As New DirectoryInfo(dir)
Dim mi As New ToolStripMenuItem(dirinfo.Name)
m.DropDownItems.Add(mi)
m.DropDownItems.AddRange(
dirinfo.GetFiles.Select(Of ToolStripMenuItem) _
(
Function(fi As FileInfo) New ToolStripMenuItem(fi.Name)
).ToArray())
AddSubDirsToMenuRecursive(mi, dirinfo.FullName)
Next
End Sub
如果您有兴趣在单击时打开文件,请将m.DropDownItems.AddRange
块替换为
m.DropDownItems.AddRange(
dirinfo.GetFiles.Select(Of ToolStripMenuItem) _
(Function(fi As FileInfo)
Dim mi1 = New ToolStripMenuItem(fi.Name)
AddHandler mi1.Click,
Sub(sender As Object, e As EventArgs)
Process.Start(fi.FullName)
End Sub
Return mi1
End Function
).ToArray())