How to use EnumerateFiles RRS feed

  • Question

  • msdn says:about EnumerateFiles:

    The EnumerateFiles and GetFiles methods differ as follows: When you use EnumerateFiles, you can start enumerating the collection of names before the whole collection is returned; when you use GetFiles, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient.

    How can I start using the collection before the whole collection is returned?

    The following code gives an elapsed time of more than 3 minutes for a directory with around 45000 files

    Dim TIme1, TIme2 As String

    TIme1 = TimeString

    Dim DirFiles As Generic.List(Of String) = New Generic.List(Of String)(Directory.EnumerateFiles(SourceDirectory))

    Dim NumberOfFiles As Integer

    NumberOfFiles = DirFiles.Count()

    TIme2 = TimeString

    MsgBox("Begin time " & TIme1 & "There are " & NumberOfFiles & " Photos in the Directory ." & SourceDirectory & "End Time " & TIme2)

    Can I already use entries in Dirfiles before the collection is entirely read? How?

    WHY: I try to develop a screensaver that will display RANDOM images from a directory that will grow to more than 50000 images. The screensaver should show the first image after 1 or 2 seconds. I guess I must use EnumerateFiles, or is there another way to read files randomly from a directory?

    Thanks for any useful information and suggestions.

    Kind regards


    • Moved by litdev Monday, August 3, 2015 3:31 PM
    Thursday, July 30, 2015 3:36 AM


All replies

  • This doesn't look like a question about the Small Basic language. Are you in the right forum?

    Jan [ WhTurner ] The Netherlands

    Thursday, July 30, 2015 8:45 AM
  • Hello,

    Please ask in the proper language forum in the Visual Studio Languages forums on MSDN.


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join('6D73646E5F6B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    • Proposed as answer by Mike Laughlin Wednesday, August 5, 2015 3:01 PM
    • Marked as answer by Dave PatrickMVP Thursday, August 13, 2015 3:16 PM
    Wednesday, August 5, 2015 2:59 PM