private void UnTrack(DirectoryWatcherItem watcher, bool removeWatcherFromGlobals)
        {
            foreach (var child in ListTrackedChildren(watcher))
            {
                UnTrack(child, true);
            }

            watcher.TrackCount--;

            if (watcher.TrackCount == 0)
            {
                if (watcher.Watcher != null)
                {
                    DisposeNativeWatcher(watcher.Watcher);
                    watcher.Watcher = null;
                }

                watcher.Parent = null;

                if (removeWatcherFromGlobals)
                {
                    watchers.Remove(watcher.Path);
                }
            }
        }
        private DirectoryWatcherItem Track(DirectoryInfo info, bool watcherNode)
        {
            DirectoryWatcherItem watcher;

            if (watchers.TryGetValue(info.FullName, out watcher))
            {
                if (watcher.Watcher == null && watcherNode)
                {
                    watcher.Watcher = CreateFileSystemWatcher(watcher.Path);
                }
                watcher.TrackCount++;
                return(watcher);
            }

            var parent = info.Parent != null?Track(info.Parent, false) : null;

            if (parent != null && watcherNode)
            {
                if (parent.Watcher != null)
                {
                    return(parent);
                }

                var childrenDirectoryList = parent.ListChildrenDirectories().ToList();
                var countTracked          = CountTracked(childrenDirectoryList);

                var newCount = (countTracked + 1);
                if (newCount == childrenDirectoryList.Count && newCount > 1)
                {
                    UnTrack(parent, false);
                    parent.Watcher = CreateFileSystemWatcher(parent.Path);
                    return(parent);
                }
            }

            watcher = new DirectoryWatcherItem(info)
            {
                Parent = parent
            };
            if (watcherNode)
            {
                watcher.Watcher = CreateFileSystemWatcher(watcher.Path);
            }
            watchers.Add(watcher.Path, watcher);

            watcher.TrackCount++;
            return(watcher);
        }
 private IEnumerable <DirectoryWatcherItem> ListTrackedChildren(DirectoryWatcherItem watcher)
 {
     return(ListTracked(watcher.ListChildrenDirectories()));
 }
        private void UnTrack(DirectoryWatcherItem watcher, bool removeWatcherFromGlobals)
        {
            foreach (var child in ListTrackedChildren(watcher))
            {
                UnTrack(child, true);
            }

            watcher.TrackCount--;

            if (watcher.TrackCount == 0)
            {
                if (watcher.Watcher != null)
                {
                    DisposeNativeWatcher(watcher.Watcher);
                    watcher.Watcher = null;
                }

                watcher.Parent = null;

                if (removeWatcherFromGlobals)
                {
                    watchers.Remove(watcher.Path);
                }
            }
        }
        private DirectoryWatcherItem Track(DirectoryInfo info, bool watcherNode)
        {
            DirectoryWatcherItem watcher;
            if (watchers.TryGetValue(info.FullName, out watcher))
            {
                if (watcher.Watcher == null && watcherNode)
                {
                    watcher.Watcher = CreateFileSystemWatcher(watcher.Path);
                }
                watcher.TrackCount++;
                return watcher;
            }

            var parent = info.Parent != null ? Track(info.Parent, false) : null;

            if (parent != null && watcherNode)
            {
                if (parent.Watcher != null)
                {
                    return parent;
                }

                var childrenDirectoryList = parent.ListChildrenDirectories().ToList();
                var countTracked = CountTracked(childrenDirectoryList);

                var newCount = (countTracked + 1);
                if (newCount == childrenDirectoryList.Count && newCount > 1)
                {
                    UnTrack(parent, false);
                    parent.Watcher = CreateFileSystemWatcher(parent.Path);
                    return parent;
                }
            }

            watcher = new DirectoryWatcherItem(info) { Parent = parent };
            if (watcherNode)
            {
                watcher.Watcher = CreateFileSystemWatcher(watcher.Path);
            }
            watchers.Add(watcher.Path, watcher);

            watcher.TrackCount++;
            return watcher;
        }
 private IEnumerable<DirectoryWatcherItem> ListTrackedChildren(DirectoryWatcherItem watcher)
 {
     return ListTracked(watcher.ListChildrenDirectories());
 }