OsherLandes
Member
ok so basicly ive been attempting to make an application in wich the user puts a file path into a textbox and then clicks a button and the all the subdirectories adn the subdirectories of the subdirectories of the subdirectories etc... are added to a listbox below. It all seems to be working fine but i cant help thinking that the code may not actually be getting all the directories and subdirectories so im going to post my code in the hope that people can point out my errors to me:
Imports System.IO
Imports System.Windows.Forms.Application
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear() 'clear listbox
Dim index2 As Integer = 0 'declare index
'check for input
If TextBox1.Text = "" Then
MsgBox("No Directory Entered")
Else
'check if path exists
If System.IO.Directory.Exists(TextBox1.Text) = False Then
MsgBox("Directory Does Not Exist")
Else
'add original path to list of directories
ListBox1.Items.Add(TextBox1.Text)
'declare variables
Dim startdirs() As String = Directory.GetDirectories(TextBox1.Text)
Dim dirs(9999999) As String
Dim roots(9999999) As String
'load the first directories to check inte the variable dirs to begin loop
For i As Integer = 0 To startdirs.GetUpperBound(0)
dirs(i) = startdirs(i)
ListBox1.Items.Add(dirs(i))
Next
Do
DoEvents()
For i1 As Integer = 0 To dirs.GetUpperBound(0)
DoEvents()
If dirs(i1) = "" Then
Exit For
End If
Try
'get root directorys
Dim rootsofdir() As String = Directory.GetDirectories(dirs(i1))
For i As Integer = 0 To rootsofdir.GetUpperBound(0)
'add subdirectories of directory to list of subdirectories
roots(index2) = rootsofdir(i)
index2 += 1
If rootsofdir(i) = "" Then
index2 -= 1
End If
Next
'catch exception of access denied
Catch ex As UnauthorizedAccessException
MsgBox(ex.Message)
End Try
Next
index2 = 0 ' reset index
For i As Integer = 0 To roots.GetUpperBound(0)
DoEvents()
'add list of subdirectories to listbox
If roots(i) = "" Then
Exit For
End If
ListBox1.Items.Add(roots(i))
Next
For i As Integer = 0 To dirs.GetUpperBound(0)
DoEvents()
'make sure array dirs() is clear
If dirs(i) = "" Then
Exit For
End If
dirs(i) = ""
Next
For i As Integer = 0 To roots.GetUpperBound(0)
DoEvents()
'add contents of roots(subdirectoies) to dirs to be checked
If roots(i) = "" Then
Exit For
End If
dirs(i) = roots(i)
'cleard roots array as it goes along
roots(i) = ""
Next
index2 = 0 ' reset index2
Loop Until dirs(0) = "" 'end when no more directories are left
MsgBox("Finished")
End If
End If
End Sub
End Class
Imports System.IO
Imports System.Windows.Forms.Application
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear() 'clear listbox
Dim index2 As Integer = 0 'declare index
'check for input
If TextBox1.Text = "" Then
MsgBox("No Directory Entered")
Else
'check if path exists
If System.IO.Directory.Exists(TextBox1.Text) = False Then
MsgBox("Directory Does Not Exist")
Else
'add original path to list of directories
ListBox1.Items.Add(TextBox1.Text)
'declare variables
Dim startdirs() As String = Directory.GetDirectories(TextBox1.Text)
Dim dirs(9999999) As String
Dim roots(9999999) As String
'load the first directories to check inte the variable dirs to begin loop
For i As Integer = 0 To startdirs.GetUpperBound(0)
dirs(i) = startdirs(i)
ListBox1.Items.Add(dirs(i))
Next
Do
DoEvents()
For i1 As Integer = 0 To dirs.GetUpperBound(0)
DoEvents()
If dirs(i1) = "" Then
Exit For
End If
Try
'get root directorys
Dim rootsofdir() As String = Directory.GetDirectories(dirs(i1))
For i As Integer = 0 To rootsofdir.GetUpperBound(0)
'add subdirectories of directory to list of subdirectories
roots(index2) = rootsofdir(i)
index2 += 1
If rootsofdir(i) = "" Then
index2 -= 1
End If
Next
'catch exception of access denied
Catch ex As UnauthorizedAccessException
MsgBox(ex.Message)
End Try
Next
index2 = 0 ' reset index
For i As Integer = 0 To roots.GetUpperBound(0)
DoEvents()
'add list of subdirectories to listbox
If roots(i) = "" Then
Exit For
End If
ListBox1.Items.Add(roots(i))
Next
For i As Integer = 0 To dirs.GetUpperBound(0)
DoEvents()
'make sure array dirs() is clear
If dirs(i) = "" Then
Exit For
End If
dirs(i) = ""
Next
For i As Integer = 0 To roots.GetUpperBound(0)
DoEvents()
'add contents of roots(subdirectoies) to dirs to be checked
If roots(i) = "" Then
Exit For
End If
dirs(i) = roots(i)
'cleard roots array as it goes along
roots(i) = ""
Next
index2 = 0 ' reset index2
Loop Until dirs(0) = "" 'end when no more directories are left
MsgBox("Finished")
End If
End If
End Sub
End Class