Library code snippets
List Files in a Directory
A little code snippet that lists files in a directory.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
' make a reference to a directory
Dim di As New IO.DirectoryInfo("c:\")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
'list the names of all files in the specified
directory
For Each dra In diar1
ListBox1.Items.Add(dra)
Next
End Sub
To filter search change di.GetFiles() to di.GetFiles(“.extionsion”)
Related articles
Related discussion
-
MSSQL Query in VB.Net Fails
by 7upsk (1 replies)
-
bar graphs in visual basic.net
by bhabybash (1 replies)
-
How to write the category attribut in a class dynamically
by converter2009 (1 replies)
-
VB.NET: Hide and show table using radio buttons
by converter2009 (1 replies)
-
VB.Net Button Problem
by pysdex (0 replies)
Related podcasts
-
xpert to Expert: Inside Concurrent Basic (CB)
"Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Dim basedir As New IO.DirectoryInfo(basepath)
Dim dir1 As IO.DirectoryInfo() = base_dir.GetDirectories()
Dim dir As IO.DirectoryInfo
'list the names of all files in the specified directory
For Each dir In dir1
ListBox1.Items.Add(dir)
Next
End Sub
base_path is the basic directory dimmed as String
if you want to run the code in a thread so it doesn't tie up the GUI during long searches you can do this
Dim makeNewThread As New System.Threading.Thread(AddressOf SubNameHere)
makeNewThread.Start()
put the sub routine name in place of "SubNameHere" and this should contain the code to start off the recursive search function.
This file search seems really nice. Do you know what library to add in the project Reference in order to use File I/O?
I'm getting errors here:
Dim DI As New IO.DirectoryInfo
Dim myFile As IO.FileInfo() = DI.GetFiles()
Dim DFI As IO.FileInfo
Errors say "User-defined type not defined"
Thanks a bunch, I really appreciate it! I've looked under the Reference list for the past 2 hours and still couldn't find File I/O system lib or some sort.
put this line in the top of your project
Imports System.IO
What parameter do I supply for Array when calling the sub?
define an arraylist and a string in your calling routine like this
Dim tarray As New ArrayList()
Dim SearchPath As String
SearchPath = "C:\"
Then call the subroutine like this
RecursiveSearch(SearchPath, tarray)
obviously you don't need to set tarray to anything on the first call.
What parameter do I supply for Array when calling the sub?
I'm getting errors here:
Dim DI As New IO.DirectoryInfo
Dim myFile As IO.FileInfo() = DI.GetFiles()
Dim DFI As IO.FileInfo
Errors say "User-defined type not defined"
Thanks a bunch, I really appreciate it! I've looked under the Reference list for the past 2 hours and still couldn't find File I/O system lib or some sort.
Hi.
There is a problem, if the directory contains a lot of sub directories, the CPU cycles seem to max out, leaving you in a frozen state for quite a while, while the program waits to get all directory info.
Any ideas how to minimise this?
Thanks
mark.
i ran the whole thing in a new thread and that makes it "run in the background" so to speak, so the application doesn't hang up while it's processing.
The only problem with using a thread is it leaves the application open to other processes at the same time.
Very nicely done my friend. This is the cleanest way I have seen a VB.NET directory listing so far.
Hi.
There is a problem, if the directory contains a lot of sub directories, the CPU cycles seem to max out, leaving you in a frozen state for quite a while, while the program waits to get all directory info.
Any ideas how to minimise this?
Thanks
mark.
Get all the files in the directory, in each subdirectory, and their subdirectories etc...
' This is a function to get all the files in a directory. This will get the files
' in all subdirectories of the parent folder
Private Sub RecursiveSearch(ByRef strDirectory As String, ByRef array As ArrayList)
Dim dirInfo As New IO.DirectoryInfo(strDirectory)
' Try to get the files for this directory
Dim pFileInfo() As IO.FileInfo
Try
pFileInfo = dirInfo.GetFiles()
Catch ex As UnauthorizedAccessException
MessageBox.Show(ex.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
' Add the file infos to the array
array.AddRange(pFileInfo)
' Try to get the subdirectories of this one
Dim pdirInfo() As IO.DirectoryInfo
Try
pdirInfo = dirInfo.GetDirectories()
Catch ex As UnauthorizedAccessException
MessageBox.Show(ex.Message, "Exception!", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
' Iterate through each directory and recurse!
Dim dirIter As IO.DirectoryInfo
For Each dirIter In pdirInfo
RecursiveSearch(dirIter.FullName, array)
Next dirIter
End Sub
this is just what i was looking for. is it the same for getting directories? i tryed changing di.getfiles() to di.getdirectories() but i get an error
in order to get all the directories in the "main" directory use:
' make a reference to a directory
Dim di As New IO.DirectoryInfo(DriveLetter & ":\")
Dim diar1 As IO.DirectoryInfo() = di.GetDirectories
Dim dra As IO.DirectoryInfo
'list the names of all directories in the base drive
For Each dra In diar1
MessageBox.Show(dra.ToString)
Next
End Sub
of course you can change it so it uses a listbox instead of a messagebox or whatnot, but that will show all the base directories.
one change to make tho, is the Io.DirectoryInfo should be the drive letter... ie.
instead of:
you should write:
substituting C:\ for the drive of your choice.
NOTE: This does not get the subdirectories. I'm going to work on some code to do that.
' make a reference to a directory
Dim di As New IO.DirectoryInfo(DriveLetter & ":\")
Dim diar1 As IO.DirectoryInfo() = di.GetDirectories
Dim dra As IO.DirectoryInfo
'list the names of all directories in the base drive
For Each dra In diar1
MessageBox.Show(dra.ToString)
Next
End Sub
of course you can change it so it uses a listbox instead of a messagebox or whatnot, but that will show all the base directories.
one change to make tho, is the Io.DirectoryInfo should be the drive letter... ie.
instead of:
you should write:
substituting C:\ for the drive of your choice.
NOTE: This does not get the subdirectories. I'm going to work on some code to do that.
For instance, retrieving all usercontrols from a directoy:
As for the directory (check if it exists), use the following:
Dim myDirectory As New IO.DirectoryInfo("C:\temp")
If myDirectory.Exists = True Then
'directory exists
Else
'directory does NOT exisist
End if
Stephan
this is just what i was looking for. is it the same for getting directories? i tryed changing di.getfiles() to di.getdirectories() but i get an error
This thread is for discussions of List Files in a Directory.