private void UriTextbox_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { var directory = new DirectoryInfo(UriTextbox.Text); var lv = new List <string>(); lv.Add(directory.FullName); while (directory.Parent != null) { lv.Add(directory.Parent.FullName); directory = directory.Parent; } var items = TreeListItemsSource.First().Children; for (int i = lv.Count - 1; i >= 0; i--) { directory = new DirectoryInfo(lv[i]); if (directory.Exists) { var selected = items.FirstOrDefault(o => o.FullPath == directory.FullName); if (selected == null) { if (directory.Parent == null) { TreeListItemsSource.Add(new TreeItem(IconType.Drive, directory.FullName, directory.Name)); } else { TreeListItemsSource.Add(new TreeItem(IconType.Folder, directory.FullName, directory.Name)); } selected = TreeListItemsSource.FirstOrDefault(o => o.FullPath == directory.FullName); } else { selected = items.FirstOrDefault(o => o.FullPath == directory.FullName); } GetDirectories(selected); items = selected.Children; selected.IsExpanded = true; if (i == 0) { selected.IsSelected = true; } } } } }
private void DeleteFolder(string path) { var directory = new DirectoryInfo(path); var list = new List <string>(); while (directory.Parent != null) { list.Add(directory.Parent.Name); directory = directory.Parent; } var items = TreeListItemsSource.First().Children; TreeItem selected = null; for (int i = list.Count - 1; i >= 0; i--) { selected = items.FirstOrDefault(o => o.Path == list[i]); items = selected.Children; } if (MessageBox.Show(string.Format("Are sure you want to delete this folder: {0}", path), "Delete", MessageBoxButton.OKCancel) == MessageBoxResult.OK) { FileOperationAPIWrapper.Send(path); var deleted = selected.Children.FirstOrDefault(o => o.FullPath == path); // make sure to jump to prev or next if deleted is selected to prevent selected go back to root if (deleted.IsSelected && selected.Children.Count > 1) { var i = selected.Children.IndexOf(deleted); if (i == selected.Children.Count - 1) { i--; } else if (i >= 0) { i++; } var nextSelected = selected.Children[i]; nextSelected.IsSelected = true; } selected.Children.Remove(deleted); } }