Display the contents of a folder in a listbox?

P

Paul H

I have developed several databases in Access. I know VBA. I am making the
jump to VB and am currently using Visual basic 2005 Express.

Can anyone give me a snippet that will show me how to list the contents of a
directory in a listbox.

If you could also point me toward a good beginners VB 2005 book or website I
would appreciate it.

Regards,

Paul
 
L

Lucky

hi,
i'm using VS2003 as VS2005 is still in beta stage. but you can use
this code in both version.

first you need to import this name space :

Imports System.IO

than you can paste this code in a procedure or in Load event of the
form :

Dim filInfo As FileInfo
For Each fil As String In Directory.GetFiles("d:\temp")
filInfo = New FileInfo(fil)

ListBox1.Items.Add(filInfo.Name)
Next

Dim DirInfo As DirectoryInfo
For Each dir As String In Directory.GetDirectories("d:\temp")
DirInfo = New DirectoryInfo(dir)
ListBox1.Items.Add("[DIR] " & DirInfo.Name)
Next

this will fetch info from the Directory and fill the ListBox.

and the site for the ebook. you can try this one:

http://www.ingenieriauai.com.ar/eBooks/
PW: 01007011

i hope this will help you.
 
H

Herfried K. Wagner [MVP]

Paul H said:
I have developed several databases in Access. I know VBA. I am making the
jump to VB and am currently using Visual basic 2005 Express.

Can anyone give me a snippet that will show me how to list the contents of
a directory in a listbox.

\\\
Imports System.IO
....
For Each FileName As String In Directory.GetFiles(...)
Me.ListBox1.Items.Add(Path.GetFileName(FileName))
Next FileName
///
 
H

Homer J Simpson

Paul H said:
I have developed several databases in Access. I know VBA. I am making the
jump to VB and am currently using Visual basic 2005 Express.

Can anyone give me a snippet that will show me how to list the contents of
a directory in a listbox.

If you could also point me toward a good beginners VB 2005 book or website
I would appreciate it.

I'm still playing with this but:

Dim S As String, I As Integer, L As ListViewItem, J As Long, T As Date
For I = 0 To CheckedListBox1.Items.Count - 1
S = CheckedListBox1.Items(I)
dlgOpenFile.InitialDirectory = S
For Each foundFile As String In My.Computer.FileSystem.GetFiles( _
S, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
ListView2.Items.Add(foundFile)
L = ListView2.Items(ListView2.Items.Count - 1)
L.Checked = True
Next

I'm using a ListView but a Listbox works too.
 
G

Guest

Ok doing something like this to fill a listview. The first time the following
line of code runs it takes like 15-20 seconds. If I open the form again it
takes about 5 seconds. If I stop the app and wait 15 minutes or so and start
the app in debug again the delay is there, but if start the app in debug
immediatly after stopping a debug session there is no delay.
Dim files() As System.IO.FileInfo = dirInfo.GetFiles("*.nc")
 
H

Homer J Simpson

Ok doing something like this to fill a listview. The first time the
following
line of code runs it takes like 15-20 seconds. If I open the form again it
takes about 5 seconds. If I stop the app and wait 15 minutes or so and
start
the app in debug again the delay is there, but if start the app in debug
immediatly after stopping a debug session there is no delay.
Dim files() As System.IO.FileInfo = dirInfo.GetFiles("*.nc")

It seems that Windows does some sort of storing/buffering of directories, so
sometimes the info may come from a buffer, sometimes it re-reads it.

I notice that the CD/DVD drive is particularly cranky this way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top