ListBox directory

Lanc1988

Well-known member
Joined
Nov 27, 2003
Messages
508
I have a list box and I want it to add all items in a specified folder.

I have this so far, but I cant figure out how to get the filenames part right:

ListBox1.Items.Add(Application.StartupPath & "\Program Images\Image Viewer\" & filename.Text)

Also, when its displaying that in the listbox, its showing the whole directory (ex. C:\Files\Folder\) I would like it to just show the filename with the ending (ex. Picture.bmp or Text.txt)

Thanks in advance
 
hmm.. I tried this:

ListBox1.Items.Add(System.IO.Directory.GetFiles(Application.StartupPath & "\Program Images\Image Viewer\"))

And now it displays System String[] in the list box and when clicked still doesnt show the file..
Am i using the System.IO.Directory.GetFiles() function right? if not whats wrong?
 
And now it displays System String[] in the list box and when clicked still doesnt show the file..
Am i using the System.IO.Directory.GetFiles() function right? if not whats wrong?

This is because the method GetFiles() returns an array of strings not a single item and the Add method you are using accepts one item only. Thats why as IcePlug said use the AddRange method instead of the Add.

You can always loop through the array and Add the items one by one if you prefer, but I would go with the AddRange....
 
Ok its works perfectly, except it still shows the whole file path to the file. Is there something I can add to it to just display the file name in the list (Example: File.htm or Picture.bmp)
 
Originally posted by Lanc1988
Ok its works perfectly, except it still shows the whole file path to the file. Is there something I can add to it to just display the file name in the list (Example: File.htm or Picture.bmp)

ya, use the FileInfo class and the Name property.
 
It under lines it all in blue if i put this:


ListBox1.Items.AddRange(System.IO.Path.GetFileName(Application.StartupPath & "\Program Images\Image Viewer\"))
 
AddRange takes an Array which GetFileName does not return.

try something like this (C#)

foreach(string filename in System.IO.Directory.GetFiles(Application.StartupPath & "\Program Images\Image Viewer\"))

ListBox1.Items.Add(System.IO.Path.GetFileName(filename));
 
I only have vb.net 2003

I want it to get a list of all files in the specified folder tho, i dont want to enter each file thats suppose to be in it cuz i want the user to be able to add files to the folder and view them through the program.
 
Yes, you use GetFiles to get your array of files, the GetFileName on all those entries to convert them to just the filename instead of the whole path. Then you feed that array to the listboxs AddRange function.
 
try this...
Code:
        Dim Allfiles As String() = IO.Directory.GetFiles("C:\")
        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
 
Works very nice, its perfect.

Now if this is possible, is there a way to only allow certain files to be in the list? Like all image files and .html files? If its to complicated, just post that it is otherwise if its farly simple please post it, thx.
 
The GetFiles method can take a 2nd parameter to specifiy the file extension.

Code:
dim htmlfiles as string = IO.Directory.GetFiles("C:\","*.html")
 
Back
Top