From cfce111383cedd5709c0237a976fd45e8fd3aacd Mon Sep 17 00:00:00 2001 From: aloisdg Date: Sun, 31 May 2015 16:37:28 +0200 Subject: [PATCH] Handle drag and drop of folder. Fix #15 --- CountPages/ViewModel/MainViewModel.cs | 60 +++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/CountPages/ViewModel/MainViewModel.cs b/CountPages/ViewModel/MainViewModel.cs index 0247ae2..667e347 100644 --- a/CountPages/ViewModel/MainViewModel.cs +++ b/CountPages/ViewModel/MainViewModel.cs @@ -1,4 +1,7 @@ using System; +using System.Collections; +using System.Collections.Generic; +using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -34,26 +37,25 @@ public MainViewModel() private async void ExecuteDropped(object o) { var e = o as DragEventArgs; - if (e != null) - await ReadFiles(e); + if (e != null && e.Data.GetDataPresent(DataFormats.FileDrop, false)) + await ReadFiles((string[])e.Data.GetData(DataFormats.FileDrop)); } - private async Task ReadFiles(DragEventArgs e) + private async Task ReadFiles(IEnumerable droppedPaths) { - if (!e.Data.GetDataPresent(DataFormats.FileDrop)) return; //try //{ - var paths = (string[])e.Data.GetData(DataFormats.FileDrop); + + var paths = droppedPaths.SelectMany(path => path.GetAllFiles()).ToArray(); var tasks = new Task[paths.Length]; for (var i = 0; i < paths.Length; i++) { var path = paths[i]; - var stream = new FileStream(path, FileMode.Open); - tasks[i] = Task.Run(() => - new Document(path, stream)); + var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite); + tasks[i] = Task.Run(() => new Document(path, stream)); } - var documents = await Task.WhenAll(tasks); + var documents = await Task.WhenAll(tasks); PageCount = Convert.ToUInt32(documents.Sum(doc => doc.Count)); @@ -73,4 +75,44 @@ private void SwitchLoaderVisibility() RaisePropertyChanged("LoaderVisibility"); } } + + + public static class TreeReader + { + public static IEnumerable GetAllFiles(this string path) + { + var queue = new Queue(); + queue.Enqueue(path); + while (queue.Count > 0) + { + path = queue.Dequeue(); + if (String.IsNullOrEmpty(Path.GetExtension(path))) + { + try + { + foreach (var subDir in Directory.GetDirectories(path)) + queue.Enqueue(subDir); + } + catch (Exception ex) + { + Console.Error.WriteLine(ex); + } + string[] files = null; + try + { + files = Directory.GetFiles(path); + } + catch (Exception ex) + { + Console.Error.WriteLine(ex); + } + if (files != null) + foreach (var t in files) + yield return t; + } + else + yield return path; + } + } + } } \ No newline at end of file