?
Solved

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

Posted on 2009-02-16
8
Medium Priority
?
770 Views
Last Modified: 2013-11-05
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 .....
0
Comment
Question by:vbMarkO
  • 4
  • 4
8 Comments
 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 23655741
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
 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 23655781
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
 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 23655785
imports namespace

Imports System.IO
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:RamanaChoudary
ID: 23655829
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
 

Author Comment

by:vbMarkO
ID: 23666887
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
 

Author Comment

by:vbMarkO
ID: 23675409
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
 

Author Comment

by:vbMarkO
ID: 23677388
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
 

Accepted Solution

by:
vbMarkO earned 0 total points
ID: 23761492
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question