How do I find out how many files are in a directory?

后端 未结 4 764
名媛妹妹
名媛妹妹 2020-12-19 00:20

I need to get a count of the number of files in a directory. I could get the names of all the files in the directory using System.IO.Directory.GetFiles() and t

4条回答
  •  自闭症患者
    2020-12-19 00:46

    So far, in most languages I came across, you get this information only by walking the folder and counting the files. I doubt there is a Windows API call to get only the count (but I can be surprised!).
    Advantage of the method: flexibility - you can filter out some file types, walk down recursively or ignore folders, etc.

    If the method is slow for you, maybe you should get a better method, like not creating an array filled with directory info (takes time to fill it! not to mention memory cost and garbage collection time) but using an iterator: a bit more work (but once you get the function, it is always there) but much more efficient.

提交回复
热议问题