WPF TreeView File System C#
Step 1) Add TreeView to XAML
<TreeView ScrollViewer.IsDeferredScrollingEnabled="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
x:Name="TreeView1"
HorizontalAlignment="Left" Height="287"
VerticalAlignment="Top" Width="489"
Margin="10,5,0,0" ToolTip="File System in WPF TreeView"
Grid.Row="01" Grid.Column="0"
BorderBrush="Beige" BorderThickness="1">
</TreeView>
ScrollViewer.VerticalScrollBarVisibility="Auto"
x:Name="TreeView1"
HorizontalAlignment="Left" Height="287"
VerticalAlignment="Top" Width="489"
Margin="10,5,0,0" ToolTip="File System in WPF TreeView"
Grid.Row="01" Grid.Column="0"
BorderBrush="Beige" BorderThickness="1">
</TreeView>
Step 2) Add Root Node Called "Computer"
note: This is the Root Node for the Tree,Logical Drives added below child nodes
Step 3) Get Logical Drives in the System from Environment class.
Note: For Removable Drives , need to check IsReady
void TreeViewDemo_Loaded(object sender, RoutedEventArgs e)
{
TreeViewItem root = new TreeViewItem() { Header = "Computer" };
TreeView1.Items.Add(root);
root.Foreground = new SolidColorBrush(Colors.BlueViolet);
foreach (String LogicalDrives in System.Environment.GetLogicalDrives())
{
System.IO.DriveInfo dInfo = new System.IO.DriveInfo(LogicalDrives);
TreeViewItem subItems = new TreeViewItem();
subItems.Foreground = new SolidColorBrush(Colors.Gold);
subItems.Header = LogicalDrives + ((dInfo.IsReady)?dInfo.VolumeLabel:String.Empty);
root.Items.Add(subItems);
if(dInfo.IsReady)
GetFilesandFolder(subItems,LogicalDrives);
}
}
{
TreeViewItem root = new TreeViewItem() { Header = "Computer" };
TreeView1.Items.Add(root);
root.Foreground = new SolidColorBrush(Colors.BlueViolet);
foreach (String LogicalDrives in System.Environment.GetLogicalDrives())
{
System.IO.DriveInfo dInfo = new System.IO.DriveInfo(LogicalDrives);
TreeViewItem subItems = new TreeViewItem();
subItems.Foreground = new SolidColorBrush(Colors.Gold);
subItems.Header = LogicalDrives + ((dInfo.IsReady)?dInfo.VolumeLabel:String.Empty);
root.Items.Add(subItems);
if(dInfo.IsReady)
GetFilesandFolder(subItems,LogicalDrives);
}
}
Step 4) Get Files and Folders of each(top level only) Logical Drives
void GetFilesandFolder(TreeViewItem root,String rootdir)
{
AddSubFolderandFiles(root,rootdir);
}
void AddSubFolderandFiles(TreeViewItem root,String rootDir)
{
foreach (String dir in System.IO.Directory.GetDirectories(rootDir))
{
try
{
TreeViewItem subFolderItem = new TreeViewItem();
subFolderItem.Header = System.IO.Path.GetFileName(dir);
subFolderItem.Items.Add("");
subFolderItem.Expanded +=subFolderItem_Expanded;
subFolderItem.Tag = dir;
root.Items.Add(subFolderItem);
subFolderItem.Foreground = new SolidColorBrush(Colors.Gold);
}
catch (Exception ex)
{
continue;
}
}
foreach (String file in System.IO.Directory.GetFiles(rootDir))
{
TreeViewItem item = new TreeViewItem();
item.Header = System.IO.Path.GetFileName(file);
item.Foreground = new SolidColorBrush(Colors.Green);
root.Items.Add(item);
}
}
Step 5) Once Top Level Directories and Files are Added , Whenever user clicks/expands on Sub folder, at that time sub folder files & folder will be added to TreeView.
{
AddSubFolderandFiles(root,rootdir);
}
void AddSubFolderandFiles(TreeViewItem root,String rootDir)
{
foreach (String dir in System.IO.Directory.GetDirectories(rootDir))
{
try
{
TreeViewItem subFolderItem = new TreeViewItem();
subFolderItem.Header = System.IO.Path.GetFileName(dir);
subFolderItem.Items.Add("");
subFolderItem.Expanded +=subFolderItem_Expanded;
subFolderItem.Tag = dir;
root.Items.Add(subFolderItem);
subFolderItem.Foreground = new SolidColorBrush(Colors.Gold);
}
catch (Exception ex)
{
continue;
}
}
foreach (String file in System.IO.Directory.GetFiles(rootDir))
{
TreeViewItem item = new TreeViewItem();
item.Header = System.IO.Path.GetFileName(file);
item.Foreground = new SolidColorBrush(Colors.Green);
root.Items.Add(item);
}
}
Step 5) Once Top Level Directories and Files are Added , Whenever user clicks/expands on Sub folder, at that time sub folder files & folder will be added to TreeView.
void subFolderItem_Expanded(object sender, RoutedEventArgs e)
{
TreeViewItem root = sender as TreeViewItem;
root.Items.Clear();
String str = root.Tag as String;
AddSubFolderandFiles(root, str);
e.Handled = true;
}
{
TreeViewItem root = sender as TreeViewItem;
root.Items.Clear();
String str = root.Tag as String;
AddSubFolderandFiles(root, str);
e.Handled = true;
}
No comments:
Post a Comment