} // end PopulateChildrenThreadSafe function /// <summary> /// /// </summary> /// <param name="parentFolder"></param> /// <param name="parentFolderTreeItem">Send this so that if a folder didn't have sub folders, we could de-expand it</param> private void PopulateChildren(FolderModel parentFolder, TreeViewItem parentFolderTreeItem) { Thread t = new Thread(() => { try { PopulateChildrenThreadSafe(parentFolder); } catch (Exception ex) { // ignore folders that had exceptions (They will de-expand) } finally { parentFolder.Busy = false; if (!parentFolder.Children.Any()) { // add back a sub folder and de-expand this.Dispatcher.Invoke(() => { parentFolder.Children.Add(new FolderModel()); parentFolderTreeItem.IsExpanded = false; // de-expand so that the user could try expanding it again (If they put something in the folder or whatever...) }); } } }); parentFolder.Busy = true; t.Start(); }
private void OpenSelectedFolderMenuItem_Click(object sender, RoutedEventArgs e) { FolderModel selectedFolder = (sender as MenuItem).DataContext as FolderModel; if (selectedFolder != null) { System.Diagnostics.Process.Start("explorer.exe", selectedFolder.Path); } }
/// <summary> /// This function is used to add folders to any sub folder, and also to add folders to the root list of folders /// </summary> /// <param name="subFolders"></param> /// <param name="name"></param> /// <param name="path"></param> private void AddFolderThreadSafe(ObservableCollection <FileSystemNodeModel> subFolders, string name, string path) { var folder = new FolderModel { Name = name, Path = path }; // add an empty child, so we can expand it folder.Children.Add(new FolderModel()); this.Dispatcher.Invoke(() => { subFolders.Add(folder); }); }
private void FolderContentControl_MouseDown(object sender, MouseButtonEventArgs e) { FolderModel selectedFolder = (sender as ContentControl).DataContext as FolderModel; if (selectedFolder != null) { this.SelectedFolder = selectedFolder; if (this.FolderSelected != null) { this.FolderSelected(this, new FolderSelectedEventArgs { SelectedFolder = selectedFolder }); } } }
private void PopulateChildrenThreadSafe(FolderModel parentFolder) { // first clear what's there this.Dispatcher.Invoke(() => { parentFolder.Children.Clear(); }); if (System.IO.Directory.Exists(parentFolder.Path)) { // enumerate shortcuts foreach (string shortcutFile in System.IO.Directory.EnumerateFiles(parentFolder.Path, "*.lnk", System.IO.SearchOption.TopDirectoryOnly)) { // alot of win32 encapsulated stuff is going on here, so stick it in a try/catch try { string shortcutName = System.IO.Path.GetFileNameWithoutExtension(shortcutFile); string resolvedPath = Utilities.FileSystemShortcutHandler.ResolveShortcut(shortcutFile); // determining directories from this information: http://stackoverflow.com/questions/439447/net-how-to-check-if-path-is-a-file-and-not-a-directory // determine if it's a directory var fileAttrs = System.IO.File.GetAttributes(resolvedPath); // fileAttrs is a bitmask, so we have to use the & bitwise operater to determine if it's a directory or not if ((fileAttrs & System.IO.FileAttributes.Directory) == System.IO.FileAttributes.Directory) { AddFolderThreadSafe(parentFolder.Children, shortcutName, resolvedPath); } } catch (Exception ex) { // shortcut didn't work out (But I don't think we need an error message) } } // enumerate directories foreach (string directoryPath in System.IO.Directory.EnumerateDirectories(parentFolder.Path, "*.*", System.IO.SearchOption.TopDirectoryOnly)) { var dirInfo = new System.IO.DirectoryInfo(directoryPath); AddFolderThreadSafe(parentFolder.Children, dirInfo.Name, directoryPath); } List <string> extensions = new List <string>(); this.Dispatcher.Invoke(() => { // we can't access the DP inside this thread, so copy it extensions.AddRange(this.FileExtensionsList); }); // enumerate files foreach (string filePath in System.IO.Directory.EnumerateFiles(parentFolder.Path, "*.*", System.IO.SearchOption.TopDirectoryOnly)) { var fileInfo = new System.IO.FileInfo(filePath); if (extensions.Any() == false || extensions.Any(ext => string.Equals(fileInfo.Extension, '.' + ext, StringComparison.OrdinalIgnoreCase)) ) { AddFileThreadSafe(parentFolder.Children, fileInfo.Name, filePath); } } // end of looping through files } // end of if the folder exists } // end PopulateChildrenThreadSafe function