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