Search code examples
c#directory-structure

GetFiles is too slow on a Network Drive


I need to copy on a daily basis files from a Network Drive. In order to do that I have tried the following:

    var dir = new DirectoryInfo(@"Z:\");

    var filesA300 = dir.GetFiles().Where(x => x.FullName.Contains("A300") 
&& x.LastWriteTime.Date == DateTime.Now.Date).ToList();

Since the drive has thousands of files the program doesn't do anything in a useful period of time.

What are my alternatives?


Solution

  • GetFiles returns all the files to begin with before you start to filter them,

    You can use EnumerateFiles which is lazier and lets you chain your where query. You can also filter on specific file types

    Quote from the above link

    The EnumerateFiles and GetFiles methods differ as follows:

    • When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

    • When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array.

    You may also be better off doing this operation on a seperate thread so you don't block your main application thread, although this would depend on what else the application is designed to do (still may be worth while so you can stop it thinking it is unresponsive).