Listbox Problem..

Lanc1988

Well-known member
Joined
Nov 27, 2003
Messages
508
I have a listbox and it lists all the files in a directory, but for some reason it is displaying the file "thumbs.db"

I went to the directory and I didnt see it but when I click it in the listbox it wants me to Open or Save it.

So my question is, is there a way to hide a file from showing in the listbox? Or possible hiding all files ending in .db?
 
"thumbs.db" is created by Windows XP. It contains thumbnails of the images in that folder. The reason you cant see it normally is because it is a hidden system file.

Are you populating the listbox manually from code? If so, youll need to filter out hidden and system files so that they are not included.
 
ok here is the code i am using to get the files from the directory:

Code:
        Dim Allfiles As String() = IO.Directory.GetFiles(Application.StartupPath & "\Sidekick Files\Image Viewer")
        Dim strfile As String
        For Each strfile In Allfiles
            Dim filename As New IO.FileInfo(strfile) This will give you the file name etc.
            ListBox1.Items.Add(filename.Name)
        Next

        Loads the Click a Image/Help page in browser.
        AxWebBrowser1.Navigate(Application.StartupPath & "\Sidekick Files\Click a Image.htm")


So how would i go about adding code to that to make it filter out system and hidden files?
 
You can check attributes using the Attributes property provided by the FileInfo class. The enumaration has a System and Hidden members, just what you are looking for:
Code:
Dim filename As New IO.FileInfo(strfile)
if file is not system then add, do same for hidden but select the Hidden enum memmber rather than system
If Not filename.Attributes = IO.FileAttributes.System Then
ListBox1.Items.Add(filename.Name)
End If
 
when i try to use that code you posted, it doesnt show any files but instead lists the folder name.

Code:
Dim filename As New IO.FileInfo(Application.StartupPath & "\Sidekick Files\Image Viewer")
        
If Not filename.Attributes = IO.FileAttributes.System Then
ListBox1.Items.Add(filename.Name)
End If
 
Are you sure? It works for me.

And I forgot to add one thing, all files are created with the Archive atrribute so you have to add the Archive value to get the proper files.
 
does anyone know of a way where i could have it show files ending in certain types? The types would be .bmp and .jpg, but i would like to be able to add more types if i need to.

Here is the code I am using for it now:
Code:
        Dim Allfiles As String() = IO.Directory.GetFiles(Application.StartupPath & "\Sidekick Files\Image Viewer")
        Dim strfile As String
        For Each strfile In Allfiles
            Dim filename As New IO.FileInfo(strfile) This will give you the file name etc.
            ListBox1.Items.Add(filename.Name)
        Next
 
The GetFiles method accepts a search pattern as second argument. You will probably have to do a call to GetFiles for each file type.
Example pattern:
"*.bmp"

You can do something like this to include for the file attribute problem:
Code:
include archive but not hidden
Dim attrs As IO.FileAttributes = IO.FileAttributes.Archive And Not IO.FileAttributes.Hidden
If filename.Attributes = attrs Then
     add file
 
Last edited by a moderator:
Here is how you get the files up for certain types

Code:
Dim strpath As String = "c:\somedirectory"
Dim strfile As String
Dim Allfiles As String() = IO.Directory.GetFiles(strpath, "*.bmp")
For Each strfile In Allfiles
       Dim filename As New IO.FileInfo(strfile) 
            ListBox1.Items.Add(filename.Name)
        Next
 
Not sure, I tried the commas and it didnt work. What I did was just make another FOR EACH loop. So I have 2 FOR EACH loop. one does one type and the other does the other type.
 
Code:
Dim strpath As String = "c:\somedirectory"
Dim strfile As String
Dim Allfiles As String() = IO.Directory.GetFiles(strpath, "*.bmp")
For Each strfile In Allfiles
      Dim filename As New IO.FileInfo(strfile) 
       ListBox1.Items.Add(filename.Name)
 Next
Allfiles = IO.Directory.GetFiles(strpath, "*.jpg")
For Each strfile In Allfiles
      Dim filename As New IO.FileInfo(strfile) 
       ListBox1.Items.Add(filename.Name)
 Next
 
Back
Top