public async Task <List <FolderSizeInfo> > CalculateFolderSizes(string rootDirPath, bool includeSubDirs) { try { var calculatedFolderSizes = new List <FolderSizeInfo>(); if (!Directory.Exists(rootDirPath)) { return(calculatedFolderSizes); } await Task.Run(() => { var options = new EnumerationOptions { IgnoreInaccessible = true, RecurseSubdirectories = includeSubDirs }; var allFolders = Directory.GetDirectories(rootDirPath, "*", options); foreach (var folder in allFolders) { var generated = GenerateFolderInfo(folder, true); calculatedFolderSizes.Add(generated); } calculatedFolderSizes.Add(GenerateFolderInfo(rootDirPath, false)); }); var output = calculatedFolderSizes.OrderByDescending(x => x.FolderSizeInBytes).ToList(); return(output); } catch (Exception ex) { var exception = new FileServiceException( "Failure in file service", new CalculateFolderSizesException( "Size calculation for given root path failed", ex)); throw exception; } }
public async Task <int> GetCountOfSubDirectories(string rootDirPath) { try { await Task.Delay(0); var options = new EnumerationOptions { IgnoreInaccessible = true, RecurseSubdirectories = true }; var allFolders = Directory.GetDirectories(rootDirPath, "*", options); return(allFolders.Count()); } catch (Exception ex) { var exception = new FileServiceException( "Failure in file service", new CalculateFolderSizesAsyncStreamException( "Getting count of subdirectories failed", ex)); throw exception; } }
public async IAsyncEnumerable <FolderSizeInfo> CalculateFolderSizesAsyncStream(string rootDirPath, bool includeSubDirs) { bool shouldCalculate = true; try { if (!Directory.Exists(rootDirPath)) { shouldCalculate = false; } } catch (Exception ex) { var exception = new FileServiceException( "Failure in file service", new CalculateFolderSizesAsyncStreamException( "Size calculation for given root path failed", ex)); throw exception; } if (shouldCalculate) { await Task.Delay(0); var options = new EnumerationOptions { IgnoreInaccessible = true, RecurseSubdirectories = includeSubDirs }; string[] allFolders; try { allFolders = Directory.GetDirectories(rootDirPath, "*", options); } catch (Exception ex) { var exception = new FileServiceException( "Failure in file service", new CalculateFolderSizesAsyncStreamException( "Size calculation for given root path failed", ex)); throw exception; } foreach (var folder in allFolders) { FolderSizeInfo generated = null; try { generated = GenerateFolderInfo(folder, true); } catch (Exception ex) { var exception = new FileServiceException( "Failure in file service", new CalculateFolderSizesAsyncStreamException( "Size calculation for given root path failed", ex)); throw exception; } yield return(generated); } FolderSizeInfo root = null; try { root = GenerateFolderInfo(rootDirPath, false); } catch (Exception ex) { var exception = new FileServiceException( "Failure in file service", new CalculateFolderSizesAsyncStreamException( "Size calculation for given root path failed", ex)); throw exception; } yield return(root); } }