예제 #1
0
        protected bool CheckRecursive(DirectoryViewModel dir, DirectoryInfo searchedDir, out DirectoryViewModel find)
        {
            if (dir.Info.FullName.Equals(searchedDir.FullName))
            {
                find = dir;
                return(true);
            }

            foreach (var item in dir.Items.OfType <DirectoryViewModel>())
            {
                if (item.Info.FullName.Equals(searchedDir.FullName))
                {
                    find = item;
                    return(true);
                }

                DirectoryViewModel possibleFind;

                if (CheckRecursive(item, searchedDir, out possibleFind))
                {
                    find = possibleFind;
                    return(true);
                }
            }

            find = null;

            return(false);
        }
예제 #2
0
        private void SyncronizeChange(FileSystemEventArgs e)
        {
            var    attr = File.GetAttributes(e.FullPath);
            string changedDirName;
            FileSystemItemViewModel newVm;

            if (attr.HasFlag(FileAttributes.Directory))
            {
                var info = new DirectoryInfo(e.FullPath);
                newVm = new DirectoryViewModel(info);

                changedDirName = info.Parent.FullName;
            }
            else
            {
                var info = new FileInfo(e.FullPath);
                newVm = new FileViewModel(info);

                changedDirName = info.DirectoryName;
            }

            var changedDir = GetChangedDirVm(new DirectoryInfo(changedDirName));

            switch (e.ChangeType)
            {
            case WatcherChangeTypes.Changed:
            case WatcherChangeTypes.Created:
                if (!changedDir.Items.Any(x => x.Name.Equals(newVm.Name)))
                {
                    Application.Current.Dispatcher.Invoke(() => changedDir.Items.Add(newVm));
                }
                break;

            case WatcherChangeTypes.Deleted:
                Application.Current.Dispatcher.Invoke(
                    () => changedDir.Items.Remove(changedDir.Items.Single(x => x.Name.Equals(newVm.Name))));
                break;

            case WatcherChangeTypes.Renamed:
                var renEa   = e as RenamedEventArgs;
                var oldName = renEa.OldName.Split('\\').Last();

                var changedItem = changedDir.Items.Single(x => x.Name.Equals(oldName));

                if (changedItem is DirectoryViewModel)
                {
                    (changedItem as DirectoryViewModel).Info = new DirectoryInfo(renEa.FullPath);
                }
                else
                {
                    (changedItem as FileViewModel).Info = new FileInfo(renEa.FullPath);
                }

                changedItem.Invalidate();
                break;
            }
        }
예제 #3
0
        protected DirectoryViewModel GetChangedDirVm(DirectoryInfo info)
        {
            DirectoryViewModel changedDir = null;

            foreach (var r in Root)
            {
                CheckRecursive(r, info, out changedDir);
            }

            return(changedDir);
        }
예제 #4
0
        protected DirectoryViewModel ParseDirectory(DirectoryInfo info)
        {
            var dirVm = new DirectoryViewModel(info);

            foreach (var directoryInfo in dirVm.Info.EnumerateDirectories())
            {
                dirVm.Items.Add(ParseDirectory(directoryInfo));
            }

            foreach (var fileInfo in dirVm.Info.EnumerateFiles())
            {
                dirVm.Items.Add(new FileViewModel(fileInfo));
            }

            return(dirVm);
        }