ListBox1 Display Directory Folders .... ListBox2 Display files of ListBox1.SelectedItem

Been a while since I have done this but this is essentially what I need

1.  ListBox1 will display all the Folders in specified Directory .......

2.  ListBox2 will display all the files of what ever Folder is the Selected Item of ListBox1

I am using Visual Basic 2008 Express .....
vbMarkOAsked:
Who is Participating?
 
vbMarkOAuthor Commented:
I thought I would post what got it done for both

To get Folders
then to get files in the selected Folder
      For Each Dir As IO.DirectoryInfo In _
                    New IO.DirectoryInfo(dirPath).GetDirectories
         lstFolders.Items.Add(Dir)
      Next
 
      Public dirTemp As String = "" ' Placed after the Public class Form1
 
      ' Then in lstFolders_SelectedINdexChnaged event
  
      lstFiles.Items.Clear()
        Dim newPath As String = dirPath & "\" & lstFolders.SelectedItem.ToString()
        Dim folderInfo As New IO.DirectoryInfo(newPath)
        Dim arrFilesInFolder() As IO.FileInfo
        Dim fileInFolder As IO.FileInfo
 
 
 
        arrFilesInFolder = folderInfo.GetFiles("*.*")
 
        For Each fileInFolder In arrFilesInFolder
 
            lstFiles.Items.Add(fileInFolder.Name)
 
        Next
 
        dirTemp = lstFolders.SelectedItem.ToString()

Open in new window

0
 
RamanaChoudaryCommented:
1) ANS

 Dim strDirs() As String = Nothing
 Dim msAppFolders As String = "C:\vssCarpenter\"
 strDirs = Directory.GetDirectories(msAppFolders)
        For Each myfolder As String In strDirs
            ListBox1.Items.Add(myfolder)
        Next
0
 
RamanaChoudaryCommented:
2) ANSWER.

        Dim oDirInfo As DirectoryInfo = Nothing
        Dim oFileInfo() As FileInfo = Nothing

        'Reading Directory Information
        oDirInfo = New DirectoryInfo(ListBox1.SelectedItem.ToString)
        oFileInfo = oDirInfo.GetFiles("*.*??")
        If oFileInfo.Length > 0 Then
            For idx As Integer = 0 To oFileInfo.Length - 1
                ListBox2.Items.Add(oFileInfo(idx).FullName)
            Next
        End If
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
RamanaChoudaryCommented:
imports namespace

Imports System.IO
0
 
RamanaChoudaryCommented:
write 2nd one in listboxindexchanged event

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        ListBox2.Items.Clear()

        Dim oDirInfo As DirectoryInfo = Nothing
        Dim oFileInfo() As FileInfo = Nothing

        'Reading Directory Information
        oDirInfo = New DirectoryInfo(ListBox1.SelectedItem.ToString)
        oFileInfo = oDirInfo.GetFiles("*.*??")
        If oFileInfo.Length > 0 Then
            For idx As Integer = 0 To oFileInfo.Length - 1
                ListBox2.Items.Add(oFileInfo(idx).FullName)
            Next
        End If
    End Sub
0
 
vbMarkOAuthor Commented:
Sorry I havent got back to you sooner ... ok these look right ... I will give them a go and get back to you as to the results.

vbMarkO
0
 
vbMarkOAuthor Commented:
Gave these a try .... question the first one .... is it supposed to return and display in the lstFolders the full path?

If so ... then this wont work .... I need only th folder name  ...

So, I did this and it gives me just the Folder Name

        For Each Dir As IO.DirectoryInfo In _
            New IO.DirectoryInfo(dirPath).GetDirectories
            lstFolders.Items.Add(Dir)
        Next

but havnt figured out yet how to get the files ...

I couldnt make the 2nd work because in lstFoldrs all I have is a Folder name not a full path ...

If thats not supposed to happen .. I will go at it again
0
 
vbMarkOAuthor Commented:
Ok it sort o works now .... but need help here

Dim newPath As String = dirPath & "\" & lstFolders.SelectedItem ' I get an error i I try to concatenate the
' path into a string variable then use that ... it wont work ... WHY?

'////////////////////// BELOW cod worked just fine ..... but this is just not applicable in my app
 

        Dim folderInfo As New IO.DirectoryInfo("C:\Documents and Settings\Mark\My Documents\Visual Studio 2008\Projects\Code Library\Code Library\Code Library\Misc Code")
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.