steve
steve

Reputation: 23

how to check if ANY directory exists without knowing a specific directory name, but rather just the folder that the folder might be in

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).

Upvotes: 0

Views: 1558

Answers (4)

steve
steve

Reputation: 23

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?

Upvotes: 0

Adam Robinson
Adam Robinson

Reputation: 185703

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

Upvotes: 1

Anthony Pegram
Anthony Pegram

Reputation: 126982

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

Upvotes: 1

mattbasta
mattbasta

Reputation: 13709

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

Upvotes: 3

Related Questions