Link to home
Start Free TrialLog in
Avatar of vbMarkO
vbMarkO

asked on

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 .....
Avatar of RamanaChoudary
RamanaChoudary

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
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
imports namespace

Imports System.IO
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
Avatar of vbMarkO

ASKER

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
Avatar of vbMarkO

ASKER

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
Avatar of vbMarkO

ASKER

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")
ASKER CERTIFIED SOLUTION
Avatar of vbMarkO
vbMarkO

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial