Displaying List of Folders and Files using TreeView in WPF


In this article We are going to see how we can use TreeView control to display the List of Folders and Files present in a particular drive or location on your computer using WPF. Here we are using two listviews, one to display the list of Folders and other one to display the list of Files present in a folder.

In this article We are going to see how we can use TreeView control to display the List of Folders and Files present in a particular drive or location on your computer using WPF.

1. Create a WPF Application project in C# and name it as "FolderNavigationDemo".


2. Add a class to this project and name it as "Folder". This class has four properties as listed below.
a,Name: It contains the name of the folder.
b,FullPath: It contains the Full Path of the folder.
c,Files: It contains the list of files present in a directory.
d,SubFolders: It returns the subdirectories present in the current directory.

3. The code for the Folder class is shown below:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FolderNavigationDemo
{
public class Folder
{
private DirectoryInfo _folder;
private List _subFolders;
private List _files;

public Folder()
{
this.FullPath = @"C:\";

}

public string Name
{
get
{
return this._folder.Name;
}
set
{
}
}

public string FullPath
{
get
{
return this._folder.FullName;
}
set
{
if (Directory.Exists(value))
{
this._folder = new DirectoryInfo(value);
}
else
{
throw new ArgumentException("Directory must exist", "full path");
}
}
}

public List Files
{
get
{
if (this._files == null)
{
this._files = new List();
FileInfo[] fi = this._folder.GetFiles();
for (int i = 0; i < fi.Length; i++)
{
this._files.Add(fi[i]);
}
}
return this._files;
}
}

public List SubFolders
{
get
{
if (this._subFolders == null)
{
this._subFolders = new List();
DirectoryInfo[] di = this._folder.GetDirectories();
for (int i = 0; i < di.Length; i++)
{
Folder newFolder = new Folder();
newFolder.FullPath = di[i].FullName;
this._subFolders.Add(newFolder);

}
}
return this._subFolders;
}
}
}
}





4. Save the above class and then open MainWindow.xaml. In this xaml file we are going to have a TreeView Control which displays the list of Folders and Files in C: Drive using other 2 ListView controls.
The ListView "lvSubFolders" lists all the SubFolders present in a Directory and other ListView "lvFiles" displays all the Files present in a Folder.
5. Below is the code of MainWindow.xaml file:


<Window x:Class="FolderNavigationDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:FolderNavigationDemo"
Title="Folder Navigation" Height="350" Width="525">
<Window.Resources>
<ObjectDataProvider x:Key="MainFolderDataProvider">
<ObjectDataProvider.ObjectInstance>
<my:Folder FullPath="c:\"></my:Folder>
</ObjectDataProvider.ObjectInstance>
</ObjectDataProvider>
<HierarchicalDataTemplate DataType="{x:Type my:Folder}" ItemsSource="{Binding Path=SubFolders}">
<TextBox Text="{Binding Path=Name}"></TextBox>
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TreeView Grid.ColumnSpan="1" Grid.RowSpan="2" Margin="0,0,0,0" Name="trvFiles">
<TreeViewItem ItemsSource="{Binding Path=SubFolders, Source={StaticResource MainFolderDataProvider}}" Header="Folders"></TreeViewItem>
</TreeView>
<ListView Name="lvSubFolders" ItemsSource="{Binding Path=SelectedItem.SubFolders,ElementName=trvFiles, Mode=OneWay}" Grid.Column="1" Grid.RowSpan="1"></ListView>
<ListView Name="lvFiles" ItemsSource="{Binding Path=SelectedItem.Files,ElementName=trvFiles, Mode=OneWay}" Grid.Column="1" Grid.Row="1"></ListView>
</Grid>
</Window>


Article by Vaishali Jain
Miss. Jain Microsoft Certified Technology Specialist in .Net(Windows and Web Based application development)

Follow Vaishali Jain or read 127 articles authored by Vaishali Jain

Comments

Author: Vaishali Jain29 Jul 2014 Member Level: Gold   Points : 3

There are three main dll files in WPF which are responsible for generating the WPF controls:

1.PresentationCore: Contains low level classes and types that are the building blocks used by the PresentationFramework above it.
2. PresentationFramework: Contains the classes and types that makeup the API for programming in WPF.
3. WindowsBase: This assembly contains fundamental types used in building WPF programs. types such as Application and Window classes.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: