s = Environment.GetEnvironmentVariable("UserProfile") + "\\Pictures";
string[] extensions = { "*.bmp", "*.jpg", "*.png", "*.gif" };//add extensions you want to filter first
var photosfiles = extensions.SelectMany(x => Directory.GetFiles(s, x));
//string[] photosfiles = Directory.GetFiles(s, "*.*", SearchOption.AllDirectories);
for (int i = 0; i < photosfiles.ToArray().Length; i++)
{
FileInfo fi = new FileInfo((photosfiles.ToArray()[i]));
DirectoryInfo d = new DirectoryInfo(tempphotos);
long dirSize = DirSize(d);
//if copying the file would take the directory over 50MB then don't do it
if ((dirSize + fi.Length) <= 24117248)
fi.CopyTo(tempphotos + "\\" + fi.Name,true);
else
break;
}
我改变的是:
string[] extensions = { "*.bmp", "*.jpg", "*.png", "*.gif" };//add extensions you want to filter first
var photosfiles = extensions.SelectMany(x => Directory.GetFiles(s, x));
在这两行之前,我只有一行:
string[] photosfiles = Directory.GetFiles(s, "*.*", SearchOption.AllDirectories);
使用这条线它正在工作,但在更改为上面的两条线后它不起作用。
我不得不更改此行并现在添加 .ToArray()
for (int i = 0; i < photosfiles.ToArray().Length; i++)
{
FileInfo fi = new FileInfo((photosfiles.ToArray()[i]));
然后最后的结果是
dirSize + fi.Length = 19526637 和 fi.Length = 7916391
所以它跳到了休息时间。
当我第一次使用时只有一行 string[] photosfiles = Directory.GetFiles(s, " . ", SearchOption.AllDirectories);
然后没有 .ToArray()
它工作得很好,它正在复制文件然后休息;
为什么在更改为这两行并添加 .ToArray() 后它现在不起作用?