GetFiles 在网络驱动器上太慢
GetFiles is too slow on a Network Drive
我需要每天从网络驱动器复制文件。为此,我尝试了以下操作:
var dir = new DirectoryInfo(@"Z:\");
var filesA300 = dir.GetFiles().Where(x => x.FullName.Contains("A300")
&& x.LastWriteTime.Date == DateTime.Now.Date).ToList();
由于驱动器有数千个文件,程序在有用的时间内不执行任何操作。
我有什么选择?
GetFiles returns 在开始过滤之前要开始的所有文件,
您可以使用更懒惰的 EnumerateFiles,它可以让您链接 where 查询。您还可以过滤特定的文件类型
引自上文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.
您最好在单独的线程上执行此操作,这样您就不会阻塞主应用程序线程,尽管这取决于应用程序的其他设计目的(仍然可能值得,因此您可以阻止它认为它没有反应)。
你可以先枚举文件信息,然后只得到你想要的。
var myFilesToProcessInfos = new DirectoryInfo("your location").EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly).Where(x=>x.Name.Contains("Your pattern") /*&& x.CreationTime == your pattern*/);
foreach (FileInfo fInfo in myFilesToProcessInfos)
{
// do your stuff
}
我需要每天从网络驱动器复制文件。为此,我尝试了以下操作:
var dir = new DirectoryInfo(@"Z:\");
var filesA300 = dir.GetFiles().Where(x => x.FullName.Contains("A300")
&& x.LastWriteTime.Date == DateTime.Now.Date).ToList();
由于驱动器有数千个文件,程序在有用的时间内不执行任何操作。
我有什么选择?
GetFiles returns 在开始过滤之前要开始的所有文件,
您可以使用更懒惰的 EnumerateFiles,它可以让您链接 where 查询。您还可以过滤特定的文件类型
引自上文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.
您最好在单独的线程上执行此操作,这样您就不会阻塞主应用程序线程,尽管这取决于应用程序的其他设计目的(仍然可能值得,因此您可以阻止它认为它没有反应)。
你可以先枚举文件信息,然后只得到你想要的。
var myFilesToProcessInfos = new DirectoryInfo("your location").EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly).Where(x=>x.Name.Contains("Your pattern") /*&& x.CreationTime == your pattern*/);
foreach (FileInfo fInfo in myFilesToProcessInfos)
{
// do your stuff
}