TechSapphire Saturday, 2024-10-12, 0:14 AM
Site menu
Login form
News
Play Games
  • Deep Freeze
  • Ice Slide
  • Gyroball
  • Fat Fish
  • Bush Royal Rampage
  • Filling treeview with directory structure. Or Fill treeview with folder structure.

    For this purpose we have used recursive procedure.

    VB.Net sample code

    Imports System.IO

    Public Class Form1

        Dim strrootpath As String

    Private Sub btnmount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmount.Click

            FolderBrowserDialog1.ShowDialog()

            If (My.Computer.FileSystem.DirectoryExists(FolderBrowserDialog1.SelectedPath.ToString) = False) Then

                Exit Sub

            End If

            txtroot.Text = Path.GetFullPath(FolderBrowserDialog1.SelectedPath.ToString).ToString

            pathtree.Nodes.Clear()

            Dim str1 As String

            Dim str2 As String

            str1 = Path.GetFullPath(FolderBrowserDialog1.SelectedPath.ToString).ToString

            str2 = Path.GetDirectoryName(FolderBrowserDialog1.SelectedPath.ToString).ToString

            str1 = str1.Replace(str2 + "\", "")

            Dim RootNode = New TreeNode(str1)

            pathtree.Nodes.Add(RootNode)

            strrootpath = Path.GetDirectoryName(FolderBrowserDialog1.SelectedPath.ToString).ToString

            BuildDirectoryTree(RootNode, My.Computer.FileSystem.CombinePath(Path.GetDirectoryName(FolderBrowserDialog1.SelectedPath.ToString).ToString, str1))

        End Sub

        Private Sub BuildDirectoryTree(ByVal fromNode As TreeNode, ByVal basePath As String)

            Dim newDirectory As TreeNode

            Dim justTheSubdirectory As String

            pathtree.UseWaitCursor = True

     

            For Each oneDirectory As String In My.Computer.FileSystem.GetDirectories(basePath)

                justTheSubdirectory = My.Computer.FileSystem.GetName(oneDirectory)

                If (fromNode Is Nothing) Then

                    newDirectory = pathtree.Nodes.Add(justTheSubdirectory)

                Else

                    newDirectory = fromNode.Nodes.Add(justTheSubdirectory)

                End If

                BuildDirectoryTree(newDirectory, My.Computer.FileSystem.CombinePath(basePath, justTheSubdirectory))

            Next oneDirectory

            pathtree.UseWaitCursor = False

        End Sub

    End Class

    Explanation:

    For this you need button with name btnmount, textbox with name txtroot, folderbrowsedialog with name  FolderBrowserDialog1 and treeview with name pathtree.

    System.IO Namespace

    The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support.

    Categories
    Programming [27]
    Tips for programming
    Security [2]
    Security Tips
    Google [1]
    Use google faster then ever you use
    Project [14]
    HTML [2]
    Electronics [0]
    Data Structure [0]
    Database [16]
    SQL SERVER
    SSRS [1]
    Sql Server Reporting Services
    Copyright MyCorp © 2024