Reading filenames into listbox

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

How can I read into a listbox filenames of all files in a folder?

Many Thanks

Regards
 
Am 04.04.2010 22:28, schrieb John:
Hi

How can I read into a listbox filenames of all files in a folder?

lst.Items.AddRange(IO.Directory.GetFiles("c:\"))
 
Many thanks. Any way to strip the path and just get the filenames?

Thanks again

Regards
 
Hello John,

there are two options:

a)

Dim directoryInfo As New System.IO.DirectoryInfo("C:\")
Dim fileInfos() As System.IO.FileInfo

fileInfos = directoryInfo.GetFiles()
For Each fileInfo As System.IO.FileInfo In fileInfos
ListBox1.Items.Add(fileInfo.Name)
Next


b) Use the FileListBox in Microsoft.VisualBasic.Compatibility
The FileListBox has the requested functionality. However,
Microsoft encourages developers to use it only when converting
a VB6 project and not to use it for new projects. The Compatibility
library also allows the use of (some) control arrays.

To use it, follow the this procedure (please note that I use the
German version and that the menu texts are my translation to English
and therefore will (very likely) be different from those you see on
the screen.

1. In "Project Explorer" ensure that references are shown. If they
are not, please click on "Show all files".

2. Select "References" and from its context menu choose
"Add Reference..."

3. Select "Microsoft.VisualBasic.Compatibility" and click on OK.

I. In "Tool Box" right click and select "Add Tab" from the
context menu.

II. Specify a name, e.g. "Visual Basic Compatibility" and press
Enter.

III. Select this new tab and select "Add elements" from its
context menu.

IV. Type "Compatibility" into the "Filter" box.

V. Select all Elements that are listed and ensure that their
check boxes are checked.

VI. Click OK.

VII. Identify the "FileListBox" entry and draw it onto your form.


Please note that I...VII you only need to do once as it is a setting of
your Visual Studio. 1...3 you need to do for each project you want to
use it.

Best regards,

Martin


Am 05.04.2010 02:55, schrieb John:
 
Am 05.04.2010 02:55, schrieb John:
Many thanks. Any way to strip the path and just get the filenames?

I've tried it and didn't see a path. Now I've tried it again and do see a path. :-/
Should look more carefully next time... This works (VB 2008):

lst.Items.AddRange((New IO.DirectoryInfo("c:\")).GetFiles.ToArray)
 
Back
Top