private async Task GetFiles(string root, FileCountInfoModel fileCountInfoModel) { Stack <string> pending = new Stack <string>(); pending.Push(root); await Task.Run(() => { while (pending.Count != 0) { var path = pending.Pop(); string[] next = null; try { next = Directory.GetFiles(path); } catch { // ignored } if (next != null && next.Length != 0) { Parallel.ForEach(next, file => { var size = new FileInfo(file).Length / 1048576; if (size <= 10) { fileCountInfoModel.LessOrEqualTen++; return; } if (size > 10 && size <= 50) { fileCountInfoModel.LessOrEqualFifty++; return; } if (size >= 100) { fileCountInfoModel.GreaterOrEqualHundred++; } }); } try { next = Directory.GetDirectories(path); foreach (var subdir in next) { pending.Push(subdir); } } catch { // ignored } } }); }
public async Task <FileCountInfoModel> GetCountFiles(string path) { FileCountInfoModel fileCountInfoModel = new FileCountInfoModel(); await GetFiles(path, fileCountInfoModel); return(fileCountInfoModel); }