tags:

views:

136

answers:

5

In vb.net how do you check if a ANY directory exists inside a directory

I would need to know if there is a folder inside the c:\windows directory (WITHOUT knowing if there is ANY directory is in there).

+3  A: 

So you want to check to see if there are subdirectories in a directory? Fair enough:

Dim hasSubDirectories as Boolean = My.Computer.FileSystem.GetDirectories(parentDir).Count > 0
mattbasta
Please see the bottom post, ive made it an answer so you can see the new code im using. My problem is that i cant convert the directoryInfo to a string
steve
+1  A: 

You can use the DirectoryInfo class inside the System.IO namespace.

Example:

    Dim path As String = "C:\Windows"
    Dim directoryInfo As New DirectoryInfo(path)

    Dim dirInfos() As DirectoryInfo = directoryInfo.GetDirectories()

    If (dirInfos.Length > 0) Then
        ' you have directories, do what you want
    End If

    'or iterate over directories

    For Each dirInfo As DirectoryInfo In dirInfos
        ' do something with each directory
    Next
Anthony Pegram
Please see the bottom post, ive made it an answer so you can see the new code im using. My problem is that i cant convert the directoryInfo to a string
steve
+1  A: 

Rather than use a VB-specific function like mattbasta suggests, it's just as easy to use the System.IO.Directory class, which is part of the BCL and would be familiar to any other .NET developer.

Dim hasSubDirectories = System.IO.Directory.GetDirectories(parentPath).Length > 0
Adam Robinson
Please see the bottom post, ive made it an answer so you can see the new code im using. My problem is that i cant convert the directoryInfo to a string
steve
A: 

messed this one up

steve
A: 

Problem is that i cant convert to string

    Dim path As String = "..\..\..\Tier1 downloads\CourseVB\"


    If countNumberOfFolders > 0 Then 'if there is a folder then


        ' make a reference to a directory
        Dim di As New IO.DirectoryInfo(path)
        Dim diar1 As IO.DirectoryInfo() = di.GetDirectories()
        Dim dra As IO.DirectoryInfo

        'list the names of all files in the specified directory
        For Each dra In diar1

            Dim lessonDirectoryName() As Lesson
            lessonDirectoryName(0).lessonName = dra

        Next

'the the lesson is an object, and lessonName is the property of type string. How do i convert the directoryInfo to string?

steve
use dra.FullName for the full path of the directory, or dra.Name for just the name.
Adam Robinson