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

Comments

  1. 13 Feb 2009 at 08:56
    Wonderful way to do it, but how do we include more file info next to the file name on the list. or using a columns list. such as .creationtime .lastaccesstime etc etc.... Heres my code. for a list but i can't figure out adding that information next to the file name rather than under it. I'm also interested to know on adding this into a columnlist instead. I'm having a hard time. Dim Files As System.IO.DirectoryInfo = New System.IO.DirectoryInfo("Path") With Me.ListBox2.Items .Clear() .Add("Name = " & Files.Name) .Add("Created = " & Files.CreationTime) .Add("Access Time = " & Files.LastAccessTime) End With Files = Nothing
  2. 16 Nov 2005 at 14:03

    Code:

    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

  3. 09 Aug 2005 at 20:04

    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


    Code:

           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.


  4. 09 Aug 2005 at 20:01

    Quote:
    [1]Posted by martini82 on 4 Aug 2005 12:52 AM[/1]
    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


    Code:

    Imports System.IO

  5. 09 Aug 2005 at 19:58

    Quote:
    [1]Posted by hammett on 9 Aug 2005 01:38 AM[/1]
    What parameter do I supply for Array when calling the sub?


    define an arraylist and a string in your calling routine like this


    Code:

    Dim tarray As New ArrayList()
    Dim SearchPath As String


    SearchPath = "C:\"




    Then call the subroutine like this



    Code:

    RecursiveSearch(SearchPath, tarray)


    obviously you don't need to set tarray to anything on the first call.


  6. 09 Aug 2005 at 01:38

    What parameter do I supply for Array when calling the sub?

  7. 04 Aug 2005 at 00:52
    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.  

  8. 27 Jul 2005 at 13:43

    Quote:
    [1]Posted by markgrev on 15 Jun 2005 01:02 PM[/1]
    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.

  9. 08 Jul 2005 at 21:06

    Very nicely done my friend. This is the cleanest way I have seen a VB.NET directory listing so far.

  10. 15 Jun 2005 at 13:02

    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.

  11. 04 Jun 2005 at 06:57

    Get all the files in the directory, in each subdirectory, and their subdirectories etc...


    Code:

    ' 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

  12. 13 May 2005 at 17:05
    Quote:
    [1]Posted by andy.davies on 16 Feb 2004 03:54 PM[/1]
    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:

    Code:

          ' 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:
    Code:
    Dim di as new IO.DirectoryInfo(DriveLetter & ":\")

    you should write:
    Code:
    Dim di as new IO.DirectoryInfo("C:\")

    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.
  13. 13 May 2005 at 17:03
    in order to get all the directories in the "main" directory use:

    Code:

          ' 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:
    Code:
    Dim di as new IO.DirectoryInfo(DriveLetter & ":\")

    you should write:
    Code:
    Dim di as new IO.DirectoryInfo("C:\")

    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.
  14. 25 Mar 2005 at 10:55
    Using (".extention") should be be preceeded with a * when you want all files.

    For instance, retrieving all usercontrols from a directoy:

    Code:
    Dim fso As IO.FileInfo() = myDir.GetFiles("*.ascx")


    As for the directory (check if it exists), use the following:

    Code:

           Dim myDirectory As New IO.DirectoryInfo("C:\temp")
           If myDirectory.Exists = True Then
              'directory exists
           Else
              'directory does NOT exisist
           End if



    Stephan
  15. 16 Dec 2004 at 14:26
    mmm That´s weird, check your read permissions !!!
  16. 16 Feb 2004 at 15:54

    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

  17. 01 Jan 1999 at 00:00

    This thread is for discussions of List Files in a Directory.

Leave a comment

Sign in or Join us (it's free).

 JesusFreak

Related discussion

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...

We'd love to hear what you think! Submit ideas or give us feedback