Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

[3681] List Files in a Directory

Last post 11-16-2005 2:03 PM by DocStone. 15 replies.
Page 1 of 2 (16 items) 1 2 Next >
Sort Posts: Previous Next
  • 01-01-1999 12:00 AM

    [3681] List Files in a Directory

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

    • Post Points: 0
  • Advertisement

    • Red Gate Software

    Advertisement

    Want to boost your .NET application performance?

    Some developers always seem to write efficient and lightening-fast code. What is their secret? It’s ANTS Profiler. “We improved the performance of the application up to 10 times” Dan Ports, Intrigma.

    Try it for yourself now.

  • 02-16-2004 3:54 PM In reply to

    Feedback

    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
    • Post Points: 0
  • 12-16-2004 2:26 PM In reply to

    • JorgeSys
    • Not Ranked
    • Joined on 12-16-2004
    • New Member
    • Points 5

    Listing Files

    mmm That´s weird, check your read permissions !!!
    • Post Points: 0
  • 03-25-2005 10:55 AM In reply to

    change..

    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
    • Post Points: 0
  • 05-13-2005 5:03 PM In reply to

    Directory Listing

    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.
    • Post Points: 0
  • 05-13-2005 5:05 PM In reply to

    Directory LIsting

    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.
    • Post Points: 0
  • 06-04-2005 6:57 AM In reply to

    This recursive function will....

    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
    • Post Points: 0
  • 06-15-2005 1:02 PM In reply to

    • markgrev
    • Not Ranked
    • Joined on 12-13-2004
    • New Member
    • Points 10

    Problem with CPU

    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.
    • Post Points: 0
  • 07-08-2005 9:06 PM In reply to

    • digioz
    • Not Ranked
    • Joined on 07-08-2005
    • New Member
    • Points 5

    Nicely Done!

    Very nicely done my friend. This is the cleanest way I have seen a VB.NET directory listing so far.
    • Post Points: 0
  • 07-27-2005 1:43 PM In reply to

    • inertia
    • Not Ranked
    • Joined on 07-26-2005
    • Junior Member
    • Points 80
    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.
    • Post Points: 0
  • 08-04-2005 12:52 AM In reply to

    IO library

    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.  

    • Post Points: 0
  • 08-09-2005 1:38 AM In reply to

    • hammett
    • Not Ranked
    • Joined on 08-09-2005
    • New Member
    • Points 50
    What parameter do I supply for Array when calling the sub?
    • Post Points: 0
  • 08-09-2005 7:58 PM In reply to

    • inertia
    • Not Ranked
    • Joined on 07-26-2005
    • Junior Member
    • Points 80
    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.

    • Post Points: 0
  • 08-09-2005 8:01 PM In reply to

    • inertia
    • Not Ranked
    • Joined on 07-26-2005
    • Junior Member
    • Points 80
    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
    • Post Points: 0
  • 08-09-2005 8:04 PM In reply to

    • inertia
    • Not Ranked
    • Joined on 07-26-2005
    • Junior Member
    • Points 80
    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.

    • Post Points: 0
Page 1 of 2 (16 items) 1 2 Next >