Files and Directories...

kiwis

Member
Joined
Apr 23, 2007
Messages
7
Programming Experience
Beginner
VB.NET:
        ' Get all sub directories
        For Each sfolder As String In IO.Directory.GetDirectories(ssfolder)
            ListBox1.Items.Add(sfolder)
        Next
        'list all file in current directory end with .jpg
        For Each fsfolder As String In IO.Directory.GetFiles(ssfolder, "*.jpg")
            ListBox1.Items.Add(fsfolder)
        Next
I have this code, it lists all sub Directories and Files, however when a user clicks on a file I want a messagebox to show for FILES only... sub Directories it will do something else... how can i do this??
 
VB.NET:
        ' Get all sub directories
        For Each sfolder As String In IO.Directory.GetDirectories(ssfolder)
            ListBox1.Items.Add(sfolder)
        Next
        'list all file in current directory end with .jpg
        For Each fsfolder As String In IO.Directory.GetFiles(ssfolder, "*.jpg")
            ListBox1.Items.Add(fsfolder)
        Next
I have this code, it lists all sub Directories and Files, however when a user clicks on a file I want a messagebox to show for FILES only... sub Directories it will do something else... how can i do this??

VB.NET:
        ' Get all sub directories
        For Each sfolder As String In IO.Directory.GetDirectories(ssfolder)
            ListBox1.Items.Add(sfolder).Tag = "DIR"
        Next
        'list all file in current directory end with .jpg
        For Each fsfolder As String In IO.Directory.GetFiles(ssfolder, "*.jpg")
            ListBox1.Items.Add(fsfolder).Tag = "FIL"
        Next

Then inspect the .Tag property of what the user clicked on..
 
That won't do, return of Items.Add is the integer index of the added item.

Here is an option, it adds all FileInfo and DirectoryInfo objects of a path to the Listbox, when item is selected the type of object is checked to see if it is a file or folder:
VB.NET:
Private Sub LoadListbox()
    Dim di As New IO.DirectoryInfo("c:\")
    ListBox1.DataSource = di.GetFileSystemInfos
End Sub
 
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    If TypeOf ListBox1.SelectedItem Is IO.FileInfo Then
        'FILE
    Else
        'DIRECTORY
    End If
End Sub
 
That won't do, return of Items.Add is the integer index of the added item.
Indeed it does. Boo! That's too much working with TreeView that's done that to me :)

VB.NET:
Private Sub LoadListbox()
    Dim di As New IO.DirectoryInfo("c:\")
    ListBox1.DataSource = di.GetFileSystemInfos
End Sub
 
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    If TypeOf ListBox1.SelectedItem Is IO.FileInfo Then
        'FILE
    Else
        'DIRECTORY
    End If
End Sub

Legendary!
 
Back
Top