daily pastebin goal
9%
SHARE
TWEET

Untitled

a guest Mar 20th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.IO;
  15.  
  16. namespace WPF_Explorer_Tree
  17. {
  18.     /// <summary>
  19.     /// Interaction logic for Window1.xaml
  20.     /// </summary>
  21.     public partial class Window1 : Window
  22.     {
  23.         private object dummyNode = null;
  24.  
  25.         public Window1()
  26.         {
  27.             InitializeComponent();
  28.         }
  29.  
  30.         public string SelectedImagePath { get; set; }
  31.  
  32.         private void Window_Loaded(object sender, RoutedEventArgs e)
  33.         {
  34.             foreach (string s in Directory.GetLogicalDrives())
  35.             {
  36.                 TreeViewItem item = new TreeViewItem();
  37.                 item.Header = s;
  38.                 item.Tag = s;
  39.                 item.FontWeight = FontWeights.Normal;
  40.                 item.Items.Add(dummyNode);
  41.                 item.Expanded += new RoutedEventHandler(folder_Expanded);
  42.                 foldersItem.Items.Add(item);
  43.             }
  44.         }
  45.  
  46.         void folder_Expanded(object sender, RoutedEventArgs e)
  47.         {
  48.             TreeViewItem item = (TreeViewItem)sender;
  49.             if (item.Items.Count == 1 && item.Items[0] == dummyNode)
  50.             {
  51.                 item.Items.Clear();
  52.                 try
  53.                 {
  54.                     foreach (string s in Directory.GetDirectories(item.Tag.ToString()))
  55.                     {
  56.                         TreeViewItem subitem = new TreeViewItem();
  57.                         subitem.Header = s.Substring(s.LastIndexOf("\\") + 1);
  58.                         subitem.Tag = s;
  59.                         subitem.FontWeight = FontWeights.Normal;
  60.                         subitem.Items.Add(dummyNode);
  61.                         subitem.Expanded += new RoutedEventHandler(folder_Expanded);
  62.                         item.Items.Add(subitem);
  63.                     }
  64.                 }
  65.                 catch (Exception) { }
  66.             }
  67.         }
  68.  
  69.         private void foldersItem_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
  70.         {
  71.             TreeView tree = (TreeView)sender;
  72.             TreeViewItem temp = ((TreeViewItem)tree.SelectedItem);
  73.  
  74.             if (temp == null)
  75.                 return;
  76.             SelectedImagePath = "";
  77.             string temp1 = "";
  78.             string temp2 = "";
  79.             while (true)
  80.             {
  81.                 temp1 = temp.Header.ToString();
  82.                 if (temp1.Contains(@"\"))
  83.                 {
  84.                     temp2 = "";
  85.                 }
  86.                 SelectedImagePath = temp1 + temp2 + SelectedImagePath;
  87.                 if (temp.Parent.GetType().Equals(typeof(TreeView)))
  88.                 {
  89.                     break;
  90.                 }
  91.                 temp = ((TreeViewItem)temp.Parent);
  92.                 temp2 = @"\";
  93.             }
  94.             //show user selected path
  95.             MessageBox.Show(SelectedImagePath);
  96.         }
  97.  
  98.     }
  99. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top