我需要每天从网络驱动器复制文件。为了做到这一点,我尝试了以下方法:
var dir = new DirectoryInfo(@"Z:\");
var filesA300 = dir.GetFiles().Where(x => x.FullName.Contains("A300")
&& x.LastWriteTime.Date == DateTime.Now.Date).ToList();
由于驱动器有数千个文件,因此程序在一段有用的时间内不会执行任何操作。
我有什么选择?
最佳答案
GetFiles 返回开始过滤之前的所有文件,
您可以使用EnumerateFiles这是比较懒的,可以让你链接你的 where 查询。您还可以过滤特定文件类型
引自上述链接
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.
您也可能最好在单独的线程上执行此操作,这样您就不会阻塞主应用程序线程,尽管这取决于应用程序设计的其他用途(仍然可能值得,因此您可以停止它认为自己没有响应)。
关于c# - GetFiles 在网络驱动器上太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31134050/