开发者

Get subdirectories

开发者 https://www.devze.com 2023-03-06 03:18 出处:网络
I\'m trying to list all subdirectories in a directory. I wan\'t to show only the Name of the directory. For example \"Program Files\" not \"C:\\Program Files\".

I'm trying to list all subdirectories in a directory. I wan't to show only the Name of the directory. For example "Program Files" not "C:\Program Files".

This will not work for me, because it returns full paths.

Dim Dirs As String() = IO.Directory.GetDirectories("C:\")

I tried using开发者_高级运维:

Dim di As New IO.DirectoryInfo(Path)
Dim Drs As IO.DirectoryInfo = di.GetDirectories()

But it returns an error. What should I use instead?


You are getting an error because you need to store in array type:

Dim Drs() As IO.DirectoryInfo = di.GetDirectories()

You can list the directory names only using the DirectoryInfo.Name property:

For Each dr As IO.DirectoryInfo In drs
    Console.WriteLine(dr.Name)
Next


Your DirectoryInfo instances (Drs) has a Name property. That contains the name of the directory without the full path.


This doesn't compile. You should instead use:

Dim Drs() As IO.DirectoryInfo = di.GetDirectories()

di.GetDirectories() returns an array of DirectoryInfo - as, of course, you're getting directories, not a single directory.

0

精彩评论

暂无评论...
验证码 换一张
取 消