public DirectoryStructure(string drive, string rootDirectory) { Drive = drive; Root = new DirectoryStructureEntry {DirectoryName = rootDirectory}; }
public void Refresh(string parentPath, int level, Action<string> progress, bool isNew=false) { string fullPath = Path.Combine(parentPath, DirectoryName); bool exists = Directory.Exists(fullPath); if (exists) { if (level < 3) progress(fullPath); Children = new List<DirectoryStructureEntry>(Children.OrderBy(x => x.DirectoryName, LogicalStringComparer.Comparer)); long size = Directory.GetFiles(fullPath).Select(file => new FileInfo(file)).Select(info => info.Length).Sum(); foreach (var child in Children) { child.Refresh(fullPath, level+1, progress, isNew); size += child.CurrentSize; } foreach (var dir in Directory.GetDirectories(fullPath).Where(x => !IsLink(x))) { string dirName = Path.GetFileName(dir); if (!Children.Any(c => string.Compare(c.DirectoryName, dirName, true) == 0)) { var child = new DirectoryStructureEntry {DirectoryName = dirName, State = DirectoryState.Added}; try { child.Refresh(fullPath, level+1, progress, true); size += child.CurrentSize; Children.AddSorted(child, (x,y) => LogicalStringComparer.Comparer.Compare(x.DirectoryName, y.DirectoryName)); } catch (IOException) { } catch (UnauthorizedAccessException) { } } } this.CurrentSize = size; if (isNew) State = DirectoryState.Added; else if (CurrentSize == PreviousSize) State = DirectoryState.Unchanged; else State = DirectoryState.Changed; } else { this.Children.Clear(); this.CurrentSize = 0; this.State = DirectoryState.Deleted; } }