Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a list box and I want it to add all items in a specified folder.

 

I have this so far, but I can't 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

  • Leaders
Posted

Have you looked at the System.IO.Directory.GetFiles() function?

It will fill an array with all of the files in the folder. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

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 doesn't show the file..

Am i using the System.IO.Directory.GetFiles() function right? if not whats wrong?

  • Leaders
Posted
Use the .AddRange method of the ListBox. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

And now it displays System String[] in the list box and when clicked still doesn't show the file..

Am i using the System.IO.Directory.GetFiles() function right? if not whats wrong?

[/Quote]

 

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. That's 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....

Dream as if you'll live forever, live as if you'll die today
Posted
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)
Posted
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.

Posted

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));

Posted

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.

  • Leaders
Posted

try this...

       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

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...