简体   繁体   English

C#如何在不使用Directory.GetFiles()方法的情况下遍历文件列表

[英]C# How to iterate through a list of files without using Directory.GetFiles() method

I have several security cameras that upload pictures to my ftp server. 我有几个将照片上传到ftp服务器的安全摄像机。 Some of these cams conveniently create subfolders at the start of a new day in the format "yyyymmdd". 这些凸轮中的某些凸轮在新的一天开始时以“ yyyymmdd”格式方便地创建子文件夹。 This is great and makes it easy to maintain/delete older pictures by a particular day. 这很棒,并且可以在特定的日期轻松维护/删除旧照片。 Other cams aren't so nice and just dump pictures in a giant folder, making deletion difficult. 其他摄像头不是很好,只是将图片转储到一个巨大的文件夹中,因此很难删除。

So I am writing a C# windows form program to go to a specific folder (source folder) using FolderBrowserDialog and I name a target folder also using FBD. 因此,我正在编写一个C#Windows窗体程序,以使用FolderBrowserDialog转到特定文件夹(源文件夹),并且我也使用FBD命名目标文件夹。 I was using the standard process to iterate through a file list using a string array filled via Directory.GetFiles() method. 我正在使用标准过程,使用通过Directory.GetFiles()方法填充的字符串数组遍历文件列表。 I use the file's Creation Date to create a subfolder if it doesn't exist. 我使用文件的创建日期创建一个子文件夹(如果不存在)。 In either case I move the file to that date based subfolder. 无论哪种情况,我都将文件移动到基​​于该日期的子文件夹。 It works great while testing with small numbers of files. 在测试少量文件时,它的效果很好。

Now I'm ready to test against real data and I'm concerned that with some folders having thousands of files, I'm going to have many issues with memory and other problems. 现在,我准备针对真实数据进行测试,并且我担心某些文件夹中有成千上万个文件,因此我将遇到很多内存问题和其他问题。 How well can a string array handle such huge volumes of data? 字符串数组如何处理如此大量的数据? Note one folder has over 28,000 pictures. 注意一个文件夹有超过28,000张图片。 Can a string array handle such a large number of FileInfo objects? 字符串数组可以处理这么多的FileInfo对象吗?

My question then is how can I iterate through a list of files in a given folder without having to use a string array and Directory.GetFiles() method? 那么我的问题是,如何在不使用字符串数组和Directory.GetFiles()方法的情况下遍历给定文件夹中的文件列表? I'm open to any thoughts though I do want to use c# in a windows form environment. 尽管我确实想在Windows窗体环境中使用c#,但我很开放。 I have an added feature that lets me delete pictures older than a given date instead of moving them. 我有一个附加功能,可以删除早于给定日期的照片,而不用移动它们。

Many thanks! 非常感谢!

You'll be just fine with thousands of file names. 数千个文件名就可以了。 You might have a problem with millions, but thousands isn't a big deal for C#. 您可能会遇到数以百万计的问题,但对于C#而言,数以千计不是什么大问题。 You may have a performance issue just because of how NTFS works, but if so there's nothing you can do about that in C#; 您可能会因为NTFS的工作方式而遇到性能问题,但是如果是这样,则在C#中您将无能为力。 it's a problem inherent in the file system. 这是文件系统固有的问题。

However, if you really want to pick at this, you can do a little better by using Directory.EnumerateFileSystemInfos() . 但是,如果您真的想选择此选项,则可以使用Directory.EnumerateFileSystemInfos()来做得更好。 This method has two benefits over GetFiles() : GetFiles()此方法有两个好处:

  1. It loads the file name and creation date in one disk access, instead of two. 它在一个磁盘访问中而不是两个磁盘中加载文件名和创建日期。
  2. It allows you to work with an IEnumerable instead of an array, such that you only need memory for one file record at a time. 它使您可以使用IEnumerable而不是数组,从而一次只需要一个文件记录的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM