filesystem.getfiles

hello,

does anyone know any way to speed up the listing of files in vb.net 2005 This is the code i want to speed up:

for i=1 to my.computer.filesystem.getfiles(path).count
listbox1.items.add(my.computer.filesystem.getfileinfo(
my.computer.filesystem.getfiles(path).item(i-1)).name)
Next

The problem is even if I use a backgroundworker it takes too long to get all the files in a directory if there's a mass amount of data (eg: 2000-3000 files).

Thnx a lot;




Answer this question

filesystem.getfiles

  • Matt Godbolt

    In your current code you are calling the GetFiles() method multiple times, twice per itteration of the loop in fact, each time it is going out and getting the list of files… instead, why not grab a single copy at the beginning and then use it many many times ala:

    'Create collection that we will store the
    Dim fileList As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(directory)

    for i=1 to fileList.count
    listbox1.tems.add(my.computer.filesystem.getfileinfo(fileList.item(i-1)).name)
    Next

    See if that doesn’t make your life and the overall operation much faster.



  • Bob DeY

    Hi, I almsot did it in the same way... thnx anyways...

  • filesystem.getfiles