What's the best way to iterate folders and subfolders to get file size, total number of files, and total size of folder in each folder starting at a specified location?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
If you're using .NET 4, you may wish to use the
System.IO.DirectoryInfo.EnumerateDirectories
andSystem.IO.DirectoryInfo.EnumerateFiles
methods. If you use theDirectory.GetFiles
method as other posts have recommended, the method call will not return until it has retrieved ALL the entries. This could take a long time if you are using recursion.From the documentation:
Note that you will need to perform validation checks.
To iterate through all directories sub folders and files, no matter how much sub folder and files are.
then from array you can get what you want via a loop or as you want.
To iterate through files and folders you would normally use the DirectoryInfo and FileInfo types. The FileInfo type has a Length property that returns the file size in bytes.
I think you must write your own code to iterate through the files and calculate the total file size, but it should be a quite simple recursive function.
Use Directory.GetFiles(). The bottom of that page includes an example that's fully recursive, I believe.