开发者

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

开发者 https://www.devze.com 2022-12-26 23:55 出处:网络
In vb.net how do you check if a ANY directory exists inside a directory I wou开发者_Go百科ld need to know if there is a folder inside the c:\\windows directory (WITHOUT knowing if there is ANY direct

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

I wou开发者_Go百科ld need to know if there is a folder inside the c:\windows directory (WITHOUT knowing if there is ANY directory is in there).


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


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


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


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?

0

精彩评论

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

关注公众号