TechSapphire Wednesday, 2018-10-17, 1:11 PM
Site menu
Login form
News
Play Games
  • Deep Freeze
  • Ice Slide
  • Gyroball
  • Fat Fish
  • Bush Royal Rampage
  • Main » Files » Programming

    Filling treeview with directory structure Or Fill treeview with folder structure Or Populating the treeview with directory details
    2011-10-09, 8:02 AM

    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.

    Category: Programming | Added by: Admin | Tags: articles, treeview, Populate, Active, questions, Directory, programming, Populate TreeView with Active Direc, Development
    Views: 1514 | Downloads: 0 | Comments: 3 | Rating: 2.0/1
    Total comments: 0
    Only registered users can add comments.
    [ Registration | Login ]
    Categories
    Programming [3]
    .Net solution,.net project,vb project,vb,c#,.net,c language,c++,projects,project,college project,html template,free template,java
    .Net [0]
    .Net program,vb.net,c#.net,asp.net,projects,.net project,programming solution,free projects
    Mystic Programs [0]
    programming challenges,computer quiz question,secret program
    Security [0]
    Security,window security,web security,web vulnerabilities,virus,virus programming,software security,validation
    Google [0]
    Google search,Google tricks,restrict Google search,Free book search,search Ftp directly,search directory,search direct link rapidshare,search torrent directly
    Project [0]
    College projects,free project,c language project,vb project,vb.net project,.net project,c# project,html project
    Video Tutorial [2]
    Free Video Tutorial
    Database [1]
    SQL Server
    Copyright MyCorp © 2018