//listbox双击进入或打开事件 private void lb_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (e.OriginalSource.GetType() != typeof(ScrollViewer) && e.RightButton != MouseButtonState.Pressed) { var m = (MyListBoxItem)lb.SelectedItem; string filename = m.MyText; //处理查找 if (CurrentPath == "查找") { menu_open_Click(sender, e); return; } //处理当前路径+双击项 if (CurrentPath == "") //双击项为盘符 { CurrentPath = filename.Substring(filename.Length - 3, 2) + "\\"; } else if (CurrentPath.Length < 4) //当前路径为盘符,不加斜杠 { if (File.Exists(CurrentPath + filename)) //双击项为文件 { System.Diagnostics.Process.Start(CurrentPath + filename); } else { Back_History.AddLast(CurrentPath); //记录进栈 CurrentPath += filename; //双击项为目录 } } else //当前路径不为盘符加斜杠 { if (File.Exists(CurrentPath + "\\" + filename)) //双击项为文件 { System.Diagnostics.Process.Start(CurrentPath + "\\" + filename); return; } else { Back_History.AddLast(CurrentPath); //记录进栈 CurrentPath += "\\" + filename; //双击项为目录 } } //刷新listbox MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); //刷新地址栏 address_box.Text = CurrentPath; //使treeview选中项同步变化 TVChangeByOther = true; MyItemManager.GetTVItemFromPath(tv, CurrentPath, true); TVChangeByOther = false; //清空前进 if (Ahead_History.Count > 0) { Ahead_History.Clear(); } } }
//粘贴到当前路径 private void toCopy() { try { if (Clipboard.ContainsFileDropList()) { var list = Clipboard.GetFileDropList(); foreach (var i in list) { if (File.Exists(i)) { MyCopyManager.CopyFile(i, CurrentPath); } else if (Directory.Exists(i)) { MyCopyManager.CopyFolder(i, CurrentPath); var item = (tv.SelectedItem as MyTreeViewItem).Items; string n = i.Substring(i.LastIndexOf('\\') + 1); tv.BeginInit(); item.Add(new MyTreeViewItem(n, MyIcons.folder)); tv.EndInit(); } } } else { MessageBox.Show("尚未有复制的文件(夹)", "复制错误"); } } catch (Exception ee) { MessageBox.Show(ee.Message, "复制错误"); } finally { MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); } }
//treeview选中项改变事件 private void tv_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e) { if (TVChangeByOther) { return; //不来自tv本身的选择改变则直接退出 } Back_History.AddLast(CurrentPath); //记录进栈 MyTreeViewItem my_select = (MyTreeViewItem)tv.SelectedItem; CurrentPath = my_select.Tag.ToString(); List <MyListBoxItem> list = MyItemManager.CreatLBItemFromPath(CurrentPath); lb.BeginInit(); lb.Items.Clear(); foreach (var i in list) { lb.Items.Add(i); } lb.EndInit(); if (CurrentPath == "") { address_box.Text = "此电脑"; } else { address_box.Text = CurrentPath; } }
private void menu_open_dir_Click(object sender, RoutedEventArgs e) { //获取父路径 string chooes = (lb.SelectedItem as MyListBoxItem).MyMessage; string parent; if (chooes.Substring(0, 5) == "所在路径:") { parent = chooes.Substring(5); } else { var reg = new System.Text.RegularExpressions.Regex(@".*(?:\\)"); var match = reg.Match(chooes, 5); parent = match.Value; } if (parent.Length > 3) { parent = parent + "\\"; } //刷新关联的三控件 CurrentPath = parent; address_box.Text = CurrentPath; TVChangeByOther = true; MyItemManager.GetTVItemFromPath(tv, parent, true); TVChangeByOther = false; MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); address_box.IsReadOnly = false;//解锁输入框 }
//后退按钮事件 private void goback_bt_Click(object sender, RoutedEventArgs e) { if (Back_History.Count != 0) { if (address_box.IsReadOnly) { address_box.IsReadOnly = false; //解锁输入框 } //获取历史记录并移出历史记录 string his = Back_History.Last.Value; Back_History.RemoveLast(); if (CurrentPath != "查找") { Ahead_History.AddLast(CurrentPath);//当前目录移入前进 } CurrentPath = his; address_box.Text = CurrentPath == "" ? "此电脑" : CurrentPath; //刷新tv TVChangeByOther = true; MyItemManager.GetTVItemFromPath(tv, his, true); TVChangeByOther = false; //刷新listbox MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); } }
private void menu_open_Click(object sender, RoutedEventArgs e) { //获取父路径 var lt = (lb.SelectedItem as MyListBoxItem); string chooes = lt.MyMessage; string parent = chooes.Substring(5); if (chooes.Substring(0, 5) == "所在路径:") { parent = chooes.Substring(5); if (parent.Length > 3) { parent = parent + "\\"; } System.Diagnostics.Process.Start(parent + lt.MyText); } else { //刷新关联的三控件 CurrentPath = parent; address_box.Text = CurrentPath; TVChangeByOther = true; MyItemManager.GetTVItemFromPath(tv, parent, true); TVChangeByOther = false; MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); address_box.IsReadOnly = false;//解锁输入框 } }
private void menu_creat_dirs_Click(object sender, RoutedEventArgs e) { string name = "新建文件夹"; string parent; if (CurrentPath.Length < 4) { parent = CurrentPath; } else { parent = CurrentPath + "\\"; } while (Directory.Exists(parent + name)) { name += " 副本"; } Directory.CreateDirectory(parent + name); MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); //同步treeview显示 MyTreeViewItem mytv = new MyTreeViewItem(name, MyIcons.folder); var it = MyItemManager.GetTVItemFromPath(tv, CurrentPath); tv.BeginInit(); it.Items.Add(mytv); tv.EndInit(); }
//listbox子控件textbox,重命名用事件 private void TextBox_KeyDown(object sender, KeyEventArgs e) { try { if (e.Key == Key.Enter) { var item = lb.SelectedItem as MyListBoxItem; var tb = sender as TextBox; string newname = tb.Text, oldname = item.MyText, copyParent = CurrentPath; if (copyParent == "") { return; } else if (copyParent.Length > 3) { copyParent += "\\"; } if (File.Exists(copyParent + oldname)) { File.Move(copyParent + oldname, copyParent + newname); } else { Directory.Move(copyParent + oldname, copyParent + newname); } item.MyVisialbe = Visibility.Hidden; MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); } } catch (Exception) { } }
//窗口加载时初始化 private void Window_Loaded(object sender, RoutedEventArgs e) { tv.BeginInit(); MyTreeViewItem root = MyItemManager.GetRootTVItem(); tv.Items.Add(root); ((MyTreeViewItem)tv.Items[0]).IsSelected = true; tv.EndInit(); }
//listbox空白区域点击取消选中事件 private void lb_PreviewMouseDown(object sender, MouseButtonEventArgs e) { if (e.OriginalSource.GetType() == typeof(ScrollViewer)) { var item = lb.SelectedItem as MyListBoxItem; if (item != null && CurrentPath != "查找") { MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); } lb.SelectedIndex = -1; } }
private void menu_delete_Click(object sender, RoutedEventArgs e) { try { var item = lb.SelectedItem as MyListBoxItem; string name = item.MyText; string _fileinfo; if (CurrentPath.Length < 4) { _fileinfo = CurrentPath + name; } else { _fileinfo = CurrentPath + "\\" + name; } var dr = MessageBox.Show("您确认要删除吗?", "删除", MessageBoxButton.YesNo); if (dr == MessageBoxResult.No) { return; } if (File.Exists(_fileinfo)) { File.Delete(_fileinfo); MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); } else if (Directory.Exists(_fileinfo)) { //同步treeview显示 var it = MyItemManager.GetTVItemFromPath(tv, _fileinfo); tv.BeginInit(); (it.Parent as MyTreeViewItem).Items.Remove(it); tv.EndInit(); Directory.Delete(_fileinfo, true); MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); } } catch (Exception ex) { MessageBox.Show(ex.Message, "致命错误"); } }
//前进按钮事件 private void goahead_bt_Click(object sender, RoutedEventArgs e) { if (Ahead_History.Count != 0) { if (address_box.IsReadOnly) { address_box.IsReadOnly = false; //解锁输入框 } //当前目录移入历史记录 Back_History.AddLast(CurrentPath); //获取前进并移出 string go = Ahead_History.Last.Value; Ahead_History.RemoveLast(); //刷新tv TVChangeByOther = true; MyItemManager.GetTVItemFromPath(tv, go, true); TVChangeByOther = false; //刷新listbox CurrentPath = go; address_box.Text = CurrentPath == "" ? "此电脑" : CurrentPath; MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); } }
//剪切到当前路径 private void toCut() { try { string path = CurrentPath.Length > 3 ? CurrentPath : CurrentPath.Substring(0, 2); if (Clipboard.ContainsFileDropList()) { var list = Clipboard.GetFileDropList(); foreach (var i in list) { if (File.Exists(i)) { FileInfo file = new FileInfo(i); string name = i.Substring(i.LastIndexOf('\\') + 1); file.MoveTo(path + '\\' + name); } else if (Directory.Exists(i)) { DirectoryInfo dir = new DirectoryInfo(i); string name = i.Substring(i.LastIndexOf('\\') + 1); dir.MoveTo(path + "\\" + name); var item = (tv.SelectedItem as MyTreeViewItem).Items; string n = i.Substring(i.LastIndexOf('\\') + 1); tv.BeginInit(); item.Add(new MyTreeViewItem(n, MyIcons.folder)); tv.EndInit(); } } } } catch (Exception ee) { MessageBox.Show(ee.Message, "复制错误"); } finally { MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); } }
private void menu_creat_file_Click(object sender, RoutedEventArgs e) { string name = "新建文本文档.txt"; string _fileinfo; if (CurrentPath.Length < 4) { _fileinfo = CurrentPath + name; } else { _fileinfo = CurrentPath + "\\" + name; } while (File.Exists(_fileinfo)) { _fileinfo = _fileinfo.Insert(_fileinfo.Length - 4, " 副本"); } FileStream fs = new FileStream(_fileinfo, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(""); sw.Close(); MyItemManager.FlushLBByCurrentPath(lb, CurrentPath); }