/// <summary> /// Delete a item from the tree /// </summary> /// <param name="path">The path to the item</param> public void Delete(string path) { bool found = false; FileSystemTreeViewItem foundTreeViewItem = null; //Search through the existing items to try to find the one we want to delete foreach (TreeViewItem item in Items) { FileSystemTreeViewItem childFileSystemTreeViewItem = item as FileSystemTreeViewItem; if (childFileSystemTreeViewItem != null) { if (childFileSystemTreeViewItem.FileSystemItem.ExpandedPath == FileSystemItem.ExpandPath(path)) { foundTreeViewItem = childFileSystemTreeViewItem; found = true; break; } } } if (found) { //We found a item time to delete it Action action = () => { Items.Remove(foundTreeViewItem); }; Application.Current.Dispatcher.BeginInvoke(action); } }
private void SytlizeFileSystemTreeViewItem(FileSystemTreeViewItem fileSystemTreeViewItem) { //The FileSystemTreeViewItem figures out what the tree should look like this is in charge of making it look right if (fileSystemTreeViewItem != null) { //Because this is on a black background change the text to be white fileSystemTreeViewItem.Foreground = new SolidColorBrush(Colors.White); } }
/// <summary> /// Handle a rename event /// </summary> /// <param name="fileSystemTreeViewItem">The tree view</param> /// <param name="eventArguments">The event that occurred</param> /// <param name="itemAddedHandler">The delegate to invoke when a item is added</param> public static void FileWatcherRenameEventHandler(FileSystemTreeViewItem fileSystemTreeViewItem, RenamedEventArgs eventArguments, ItemAdded itemAddedHandler) { //Change a rename to a delete and add action run those actions FileSystemEventArgs addedEventArgs = new FileSystemEventArgs(WatcherChangeTypes.Created, System.IO.Path.GetDirectoryName(eventArguments.FullPath), System.IO.Path.GetFileName(eventArguments.FullPath)); FileSystemEventArgs deletedEventArgs = new FileSystemEventArgs(WatcherChangeTypes.Deleted, System.IO.Path.GetDirectoryName(eventArguments.OldFullPath), System.IO.Path.GetFileName(eventArguments.OldFullPath)); FileWatcherEventHandler(fileSystemTreeViewItem, addedEventArgs, itemAddedHandler); FileWatcherEventHandler(fileSystemTreeViewItem, deletedEventArgs, itemAddedHandler); }
private void trVwOrganizedFiles_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e) { FileSystemTreeViewItem selectedItem = e.NewValue as FileSystemTreeViewItem; //If the user selected a file from the destination get the name and set the destination file name to what you selected if (selectedItem != null && selectedItem.FileSystemItem.Type == FileSystemItem.FileSystemType.File) { txtFileName.Text = System.IO.Path.GetFileNameWithoutExtension(selectedItem.FileSystemItem.Name); } //Set the move buttons state appropriately btnMove.IsEnabled = IsMoveEnabled(); }
private void trVwFilesToOrganize_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e) { //Set the move buttons state appropriately btnMove.IsEnabled = IsMoveEnabled(); FileSystemTreeViewItem fileSystemTreeViewItem = trVwFilesToOrganize.SelectedItem as FileSystemTreeViewItem; ContextMenu menu = null; if (fileSystemTreeViewItem != null && fileSystemTreeViewItem.FileSystemItem.Type == FileSystemItem.FileSystemType.File) { menu = trVwFilesToOrganize.Resources["ctxMnuFilesToOrganize"] as System.Windows.Controls.ContextMenu; } trVwFilesToOrganize.ContextMenu = menu; }
private void TreeViewUpdated(TreeView treeView, FileSystemEventArgs eventArguments) { if (treeView == null) { return; } if (treeView.Items == null || treeView.Items.Count == 0) { return; } //Actually handle the update action that happened to the file in the tree FileSystemTreeViewItem.FileWatcherEventHandler(treeView.Items[0] as FileSystemTreeViewItem, eventArguments, this.SytlizeFileSystemTreeViewItem); }
/// <summary> /// Handle adding an item as appropriate /// </summary> /// <param name="addedTreeViewItem">The item to add</param> public void Add(FileSystemTreeViewItem addedTreeViewItem) { //Add a new item to this tree Action action = () => { Items.Add(addedTreeViewItem); if (ItemAddedHandler != null) { ItemAddedHandler(addedTreeViewItem); } }; Application.Current.Dispatcher.BeginInvoke(action); }
/// <summary> /// Populates a tree view from a directory /// </summary> /// <param name="treeView">The tree view to populate</param> /// <param name="directoryName">The root directory to get the structures from</param> /// <param name="fileNamePrefix">The file name prefix</param> /// <param name="fileNameSuffix">The file name suffix</param> /// <param name="fileProducer">The producer to get the list of files in a directory</param> private void GetDirectories(TreeView treeView, string directoryName, string fileNamePrefix, string fileNameSuffix, FileUtilities.GetFile fileProducer) { treeView.Items.Clear(); if (!Directory.Exists(directoryName)) { return; } FileSystemTreeViewItem fileSystemTreeViewItem = new FileSystemTreeViewItem(directoryName, directoryName, fileNamePrefix, fileNameSuffix, fileProducer, this.SytlizeFileSystemTreeViewItem); if (fileSystemTreeViewItem.FileSystemItem.Type != FileSystemItem.FileSystemType.Unknown) { treeView.Items.Add(fileSystemTreeViewItem); fileSystemTreeViewItem.Create(); } }
private void ctxMnuFilesToOrganize_Delete_Click(object sender, RoutedEventArgs e) { FileSystemTreeViewItem fileSystemTreeViewItem = trVwFilesToOrganize.SelectedItem as FileSystemTreeViewItem; if (fileSystemTreeViewItem != null && fileSystemTreeViewItem.FileSystemItem.Type == FileSystemItem.FileSystemType.File) { try { File.Delete(fileSystemTreeViewItem.FileSystemItem.ExpandedPath); } catch (Exception exception) { MessageBox.Show("Failed to delete file: " + fileSystemTreeViewItem.FileSystemItem.ExpandedPath + ". " + exception.Message); } } }
/// <summary> /// Handle adding an item as appropriate /// </summary> /// <param name="path">The path of the item</param> /// <param name="displayPath">The path to display to the user</param> public void Add(string path, string displayPath) { FileSystemItem file = null; if (Directory.Exists(path)) { file = new FileSystemItem(path); file.Name = Path.GetFileName(path); } else if (FileProducer != null && !FileProducer(path, FileNamePrefix, FileNameSuffix, out file)) { return; } //Add a new item to this tree Action action = () => { FileSystemTreeViewItem addedTreeViewItem = new FileSystemTreeViewItem(file.ExpandedPath, file.Name, FileNamePrefix, FileNameSuffix, FileProducer, ItemAddedHandler); if (addedTreeViewItem.FileSystemItem.Type != FileSystemItem.FileSystemType.Unknown) { bool found = false; foreach (object item in Items) { FileSystemTreeViewItem checkFileSystemTreeViewItem = item as FileSystemTreeViewItem; if (checkFileSystemTreeViewItem == null) { continue; } if (checkFileSystemTreeViewItem.FileSystemItem.Name == addedTreeViewItem.FileSystemItem.Name) { found = true; } } if (!found) { Items.Add(addedTreeViewItem); if (ItemAddedHandler != null) { ItemAddedHandler(addedTreeViewItem); } } } ; }; Application.Current.Dispatcher.BeginInvoke(action); }
private void OpenFileToOrganize() { //Open a preview up on the item that the user double clicked FileSystemTreeViewItem sourceFile = trVwFilesToOrganize.SelectedItem as FileSystemTreeViewItem; if (sourceFile != null && sourceFile.FileSystemItem.Type == FileSystemItem.FileSystemType.File) { //Copy the file clicked to the application temporary directory. This allows the file to be previewed multiple times if the //application that opens the file gets a lock on it string destinationPath = string.Empty; if (!FileUtilities.CreateAppDataTemporaryFile(_applicationData.AppTempDirectoryName, sourceFile.FileSystemItem.ExpandedPath, false, MessageHandler, RespondMessageHandler, out destinationPath)) { return; } //Start a process to handle displaying the file Process.Start(destinationPath); } }
private void trVwFilesToOrganize_KeyDown(object sender, KeyEventArgs e) { //Delete a file to sort via the keyboard if (e.Key == Key.Delete && trVwFilesToOrganize.SelectedItem != null) { FileSystemTreeViewItem treeViewItem = trVwFilesToOrganize.SelectedItem as FileSystemTreeViewItem; if (treeViewItem != null && treeViewItem.FileSystemItem.Type == FileSystemItem.FileSystemType.File) { try { File.Delete(treeViewItem.FileSystemItem.ExpandedPath); } catch (Exception exception) { MessageBox.Show("Error deleteing file " + treeViewItem.FileSystemItem.ExpandedPath + ": " + exception.Message); } } } }
/// <summary> /// Handle a file system event /// </summary> /// <param name="fileSystemTreeViewItem">The tree the event occurred on</param> /// <param name="eventArguments">The arguments that happened</param> /// <param name="itemAddedHandler">The delegate to invoke if a item is added to the tree</param> public static void FileWatcherEventHandler(FileSystemTreeViewItem fileSystemTreeViewItem, FileSystemEventArgs eventArguments, ItemAdded itemAddedHandler) { if (fileSystemTreeViewItem == null) { return; } bool correctDepth = false; string fileSystemDirectory = fileSystemTreeViewItem.FileSystemItem.ExpandedPath; string eventParentDirectoryName = System.IO.Path.GetDirectoryName(eventArguments.FullPath); correctDepth = (fileSystemDirectory == eventParentDirectoryName); //Figure out if the change happened where we are or if we should look through all our children and see if they need to do something if (!correctDepth) { Action action = () => { //We're not in the right spot, look through all our children and ask them to review the change foreach (TreeViewItem childTreeViewItem in fileSystemTreeViewItem.Items) { FileSystemTreeViewItem childFileSystemTreeViewItem = childTreeViewItem as FileSystemTreeViewItem; if (childFileSystemTreeViewItem != null) { FileWatcherEventHandler(childFileSystemTreeViewItem, eventArguments, itemAddedHandler); } } }; Application.Current.Dispatcher.BeginInvoke(action); } else { //We're in the right spot, either delete or add the item as necessary if (eventArguments.ChangeType == WatcherChangeTypes.Deleted) { fileSystemTreeViewItem.Delete(eventArguments.FullPath); } else if (eventArguments.ChangeType == WatcherChangeTypes.Created) { fileSystemTreeViewItem.Add(eventArguments.FullPath, System.IO.Path.GetFileName(eventArguments.FullPath)); } } }
/// <summary> /// Creates all the children beneath the path /// </summary> /// <param name="path">The path</param> private void Create(string path) { if (string.IsNullOrEmpty(path) || !Directory.Exists(path)) { return; } //Go through all the subdirectories in the path, doing this first makes it so the directories are at the top of the tree foreach (string subDirectory in Directory.GetDirectories(path)) { Action action = () => { FileSystemTreeViewItem subDirectoryTreeItem = new FileSystemTreeViewItem(subDirectory, System.IO.Path.GetFileName(subDirectory), FileNamePrefix, FileNameSuffix, FileProducer, ItemAddedHandler); if (subDirectoryTreeItem.FileSystemItem.Type != FileSystemItem.FileSystemType.Unknown) { //Add a node to the current tree Add(subDirectoryTreeItem); //Call create to add all children under this node recursively subDirectoryTreeItem.Create(); } }; Application.Current.Dispatcher.BeginInvoke(action); } //Invoke the delegate to return the files appropriate to this tree List <FileSystemItem> files = FileUtilities.GetFiles(FileSystemItem.ExpandedPath, FileNamePrefix, FileNameSuffix, FileProducer); //Go through all the files foreach (FileSystemItem file in files) { //Add files to the tree Action action = () => { FileSystemTreeViewItem fileTreeItem = new FileSystemTreeViewItem(file.ExpandedPath, file.Name, FileNamePrefix, FileNameSuffix, FileProducer, ItemAddedHandler); if (fileTreeItem.FileSystemItem.Type != FileSystemItem.FileSystemType.Unknown) { Add(fileTreeItem); fileTreeItem.Create(); } }; Application.Current.Dispatcher.BeginInvoke(action); } }
private bool IsMoveEnabled() { //Move is only enabled if we have a source selected, a destination selected, and they've put in a name to move it to FileSystemTreeViewItem sourceFile = trVwFilesToOrganize.SelectedItem as FileSystemTreeViewItem; if (sourceFile == null || sourceFile.FileSystemItem.Type != FileSystemItem.FileSystemType.File) { return(false); } FileSystemTreeViewItem destinationFile = trVwOrganizedFiles.SelectedItem as FileSystemTreeViewItem; if (destinationFile == null) { return(false); } if (string.IsNullOrEmpty(txtFileName.Text)) { return(false); } return(true); }
private void btnMove_Click(object sender, RoutedEventArgs e) { if (!IsMoveEnabled()) { return; } FileSystemTreeViewItem sourceFile = trVwFilesToOrganize.SelectedItem as FileSystemTreeViewItem; FileSystemTreeViewItem destinationFile = trVwOrganizedFiles.SelectedItem as FileSystemTreeViewItem; string extension = string.Empty; string moveToDirectoryName = string.Empty; extension = System.IO.Path.GetExtension(sourceFile.FileSystemItem.ExpandedPath); //The destination is the destination name that the user put in string newFileName = txtFileName.Text + extension; //Add the prefix and suffix defaults value to the new file name newFileName = FileUtilities.AddPrefixAndSuffixToFileName(newFileName, FileOrganizerSettings.Default.OrganizedFileNamePrefix, FileOrganizerSettings.Default.OrganizedFileNameSuffix); //Figure out what to do with the date bool isChecked = false; if (chkBxDated.IsChecked != null) { isChecked = (bool)chkBxDated.IsChecked; } if (isChecked) { DateTime theDate = DateTime.Now; if (dtPkrDate.SelectedDate != null) { theDate = (DateTime)dtPkrDate.SelectedDate; } //Add a date to the new file name newFileName = FileUtilities.ReplaceDateFileNamePattern(newFileName, theDate, txtPageNumber.Text); } else { //No date add the default pattern to the file name newFileName = FileUtilities.ReplaceNoDateFileNamePattern(newFileName, txtPageNumber.Text); } //Figure out which directory to move it into if (destinationFile.FileSystemItem.Type == FileSystemItem.FileSystemType.Directory) { //It's a directory just use the value moveToDirectoryName = destinationFile.FileSystemItem.ExpandedPath; } else { //Find the directory of the file that the user has selected moveToDirectoryName = System.IO.Path.GetDirectoryName(destinationFile.FileSystemItem.ExpandedPath); } //Call the API to try to move the file if (FileUtilities.MoveFile(sourceFile.FileSystemItem.ExpandedPath, System.IO.Path.Combine(moveToDirectoryName, newFileName), this.MessageHandler, this.RespondMessageHandler)) { FileSystemEventArgs eventArgs = new FileSystemEventArgs(WatcherChangeTypes.Created, moveToDirectoryName, newFileName); trVwOrganizedFileUpdated(this, eventArgs); } }