0

如何使用 VB 2010 返回一个随机子目录并在 Windows 资源管理器中打开该目录。

我有一个主目录 C:\test\ 并想从测试目录中打开或显示随机子目录的路径(作为字符串)。

理想情况下,当在 VB 表单上按下按钮时,这会将我带到随机目录。

我对 Visual Basic 很陌生,所以任何帮助都会很棒。

4

1 回答 1

1

试试下面的代码

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    'Directory info for the starting folder.
    Dim DirectoryInfo1 As New IO.DirectoryInfo("C:\test\")
    'An array of all the subdirectories of the starting folder.
    Dim Directories1() As IO.DirectoryInfo = DirectoryInfo1.GetDirectories("*", IO.SearchOption.AllDirectories)

    'Random number generator.
    Dim Random1 As New Random
    'Gets a random index from the subdirectories array.
    Dim RandomIndex1 As Integer = Random1.Next(0, Directories1.Length - 1)

    'Shows the path of a random directory.
    MsgBox(Directories1(RandomIndex1).FullName)

    'Opens the random directory inside windows explorer.
    Process.Start("explorer.exe", Directories1(RandomIndex1).FullName)
End Sub
于 2013-07-18T00:44:42.247 回答