Get subdirectories
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.
精彩评论