Cobold
Cobold

Reputation: 2613

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?

Upvotes: 4

Views: 8643

Answers (3)

mellamokb
mellamokb

Reputation: 56769

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

Upvotes: 7

Will A
Will A

Reputation: 24988

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.

Upvotes: 0

driis
driis

Reputation: 164281

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

Upvotes: 0

Related Questions