Creating Windows Explorer in WPF Using C#

Introduction

Its easy to create a woindows explorer in WPF Useing C#. This article defines how to create windows explorer in WPF using C#, to create window explorer follow the bellow steps.

Take on combo box to your  in to your windows and set the name property to cmbDirve, in the load event of windows  bind the drives available in the system with combobox . see the bellow code

private void Window_Loaded(object sender, RoutedEventArgs e)

{

    this.cmbDrive.ItemsSource = DriveInfo.GetDrives().Where(dr => dr.IsReady == true).ToList();

    this.cmbDrive.DisplayMemberPath = "Name";

    this.cmbDrive.SelectedValuePath = "Name";

}


Note:- the code 'this.cmbDrive.ItemsSource = DriveInfo.GetDrives().Where(dr => dr.IsReady == true).ToList();' Lists the Drives that is ready in the system.

Take  a Tree view controls in to your project and set the name to tvDirectories. Create a custom Tree View Item buy creating a class that inherits from Tree View Item class.

Declare two string  properties having name Path and Type that will store Name and path of directory.

Again take a List Box Control to lists contained file of current directory.

In the Select Index Changed event of Combo box clear the items of combo Box and create the object of Directory Info class. In the constructor of Directory info class pass the pass the selected value of combo box as string.

Using while loop retrieve the directories of current directory and pass to the custom tree view item and to the tree view

Add the files of current directory in to the items of List box.

Again retrieve the list of child directory, pass it to the custom tree view item and add it into the items of current tree view item.
  
Above step 5, 6 and 7 will list the Directories, child directories and files of parent directory into the tree view. See the bellow code     

      private void cmbDrive_SelectionChanged(object sender, SelectionChangedEventArgs e)

      {

           this.tvwDirectory.Items.Clear();

          DirectoryInfo DIR = new DirectoryInfo(this.cmbDrive.SelectedValue.ToString());

 

           foreach (DirectoryInfo DR in DIR.GetDirectories())

           {

                 MyTreeViewItem TVI = new MyTreeViewItem();

               TVI.Header = DR.Name;

              TVI.Path = DR.FullName;

               TVI.Expanded += new RoutedEventHandler(CTVI_Expanded);

               if (!DR.Attributes.ToString().Contains("Hidden"))

               {

                     foreach (DirectoryInfo CDIR in DR.GetDirectories())

                    {

                        MyTreeViewItem CTVI = new MyTreeViewItem();

                       CTVI.Expanded += new RoutedEventHandler(CTVI_Expanded);

                      CTVI.Header = CDIR.Name;

                      CTVI.Path = CDIR.FullName;

                        CTVI.Type = CDIR.GetType().Name;

                         TVI.Items.Add(CTVI);

                     }

                     

                  this.tvwDirectory.Items.Add(TVI);

                }

             }

             foreach (FileInfo FL in DIR.GetFiles())

            {               

               this.ltbExplorer.Items.Add(FL.Name);

          }

 

In the expand event of Tree View control again retrieve the child directories of current directory's child directory and add that in to the child items of current item's(treeview's current item)items collection. See the bellow code.

    void CTVI_Expanded(object sender, RoutedEventArgs e)

    {

        MyTreeViewItem TVI = (MyTreeViewItem)sender;

        foreach (MyTreeViewItem CTVI in TVI.Items)

        {

            if (CTVI.Type == "DirectoryInfo")

            {

                DirectoryInfo DIR = new DirectoryInfo(CTVI.Path);

                foreach (DirectoryInfo CDIR in DIR.GetDirectories())

                {

                    MyTreeViewItem CTVI1 = new MyTreeViewItem();

                    CTVI1.Expanded += new RoutedEventHandler(CTVI_Expanded);

                    CTVI1.Header = CDIR.Name;

                    CTVI1.Path = CDIR.FullName;

                    CTVI1.Type = CDIR.GetType().Name;

                    if (CTVI.Items.Contains(CTVI1.Header) == false)

                        CTVI.Items.Add(CTVI1);

                }

                //foreach (FileInfo FL in DIR.GetFiles())

                //{

                //    MyTreeViewItem CTVI2 = new MyTreeViewItem();

                //    CTVI2.Expanded += new RoutedEventHandler(CTVI_Expanded);

                //    CTVI2.Header = FL.Name;

                //    CTVI2.Path = FL.FullName;

                //    CTVI2.Type = FL.GetType().Name;

                //    CTVI.Items.Add(CTVI2);

                //}

            }

        }

        e.Handled = true;

    }

 

 In the selected changed event of tree view controls retrieve the files of current directory and add that in to the items of list box.

    private void tvwDirectory_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)

    {

        if (e.NewValue != null)

        {

            this.ltbExplorer.Items.Clear();

            MyTreeViewItem TVI = (MyTreeViewItem)e.NewValue;

            DirectoryInfo DIR = new DirectoryInfo(TVI.Path);

            foreach (FileInfo FL in DIR.GetFiles())

            {

                //MyTreeViewItem CTVI2 = new MyTreeViewItem();

                //CTVI2.Expanded += new RoutedEventHandler(CTVI_Expanded);

                //CTVI2.Header = FL.Name;

                //CTVI2.Path = FL.FullName;

                //CTVI2.Type = FL.GetType().Name;

                ltbExplorer.Items.Add(FL.Name);

            }

        }

        e.Handled = true;

    }
 
END

If any query, please feel free to comment, if you like this article please don't forget to liske.