WPF TreeView File System VB.NET
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
Private Sub TreeViewDemo_Loaded(sender As Object, e As RoutedEventArgs)
Dim root As New TreeViewItem() With { _
.Header = "Computer" _
}
TreeView1.Items.Add(root)
root.Foreground = New SolidColorBrush(Colors.BlueViolet)
For Each LogicalDrives As [String] In System.Environment.GetLogicalDrives()
Dim dInfo As New System.IO.DriveInfo(LogicalDrives)
Dim subItems As New TreeViewItem()
subItems.Foreground = New SolidColorBrush(Colors.Gold)
subItems.Header = LogicalDrives + (If((dInfo.IsReady), dInfo.VolumeLabel, [String].Empty))
root.Items.Add(subItems)
If dInfo.IsReady Then
GetFilesandFolder(subItems, LogicalDrives)
End If
Next
End Sub
Dim root As New TreeViewItem() With { _
.Header = "Computer" _
}
TreeView1.Items.Add(root)
root.Foreground = New SolidColorBrush(Colors.BlueViolet)
For Each LogicalDrives As [String] In System.Environment.GetLogicalDrives()
Dim dInfo As New System.IO.DriveInfo(LogicalDrives)
Dim subItems As New TreeViewItem()
subItems.Foreground = New SolidColorBrush(Colors.Gold)
subItems.Header = LogicalDrives + (If((dInfo.IsReady), dInfo.VolumeLabel, [String].Empty))
root.Items.Add(subItems)
If dInfo.IsReady Then
GetFilesandFolder(subItems, LogicalDrives)
End If
Next
End Sub
Step 4) Get Files and Folders of each(top level only) Logical Drives
Private Sub GetFilesandFolder(root As TreeViewItem, rootdir As [String])
AddSubFolderandFiles(root, rootdir)
End Sub
Private Sub AddSubFolderandFiles(root As TreeViewItem, rootDir As [String])
For Each dir As [String] In System.IO.Directory.GetDirectories(rootDir)
Try
Dim subFolderItem As 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 ex As Exception
Continue Try
End Try
Next
For Each file As [String] In System.IO.Directory.GetFiles(rootDir)
Dim item As New TreeViewItem()
item.Header = System.IO.Path.GetFileName(file)
item.Foreground = New SolidColorBrush(Colors.Green)
root.Items.Add(item)
Next
End Sub
AddSubFolderandFiles(root, rootdir)
End Sub
Private Sub AddSubFolderandFiles(root As TreeViewItem, rootDir As [String])
For Each dir As [String] In System.IO.Directory.GetDirectories(rootDir)
Try
Dim subFolderItem As 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 ex As Exception
Continue Try
End Try
Next
For Each file As [String] In System.IO.Directory.GetFiles(rootDir)
Dim item As New TreeViewItem()
item.Header = System.IO.Path.GetFileName(file)
item.Foreground = New SolidColorBrush(Colors.Green)
root.Items.Add(item)
Next
End Sub
Step 5) Sub Folder Expansion
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.
Private Sub subFolderItem_Expanded(sender As Object, e As RoutedEventArgs)
Dim root As TreeViewItem = TryCast(sender, TreeViewItem)
root.Items.Clear()
Dim str As [String] = TryCast(root.Tag, [String])
AddSubFolderandFiles(root, str)
e.Handled = True 'Prevent Bubble Events
End Sub
Dim root As TreeViewItem = TryCast(sender, TreeViewItem)
root.Items.Clear()
Dim str As [String] = TryCast(root.Tag, [String])
AddSubFolderandFiles(root, str)
e.Handled = True 'Prevent Bubble Events
End Sub
No comments:
Post a Comment