Using Explorer Form in vb.net

Adding directory structure to explorer form to use it like Windows Explorer:

Right click your project in Solution Explorer and click Add > Add New Item | Select Explorer Form from the list and Click ok, [keep default name if you dont know changing it in further code]

When new Form is added to your project, double click it and replace whole code with the following. Result of which will be like this-

Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.IO

Public Class Explorer1

    Private Sub Explorer1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Set up the UI
        SetUpListViewColumns()
        LoadTree()
		ListView.View = View.List
    End Sub

    Private Sub LoadTree()
        ' TODO: Add code to add items to the treeview
        Dim getInfo As System.IO.DriveInfo()
        getInfo = System.IO.DriveInfo.GetDrives
        Dim newNode As TreeNode
        For Each info As System.IO.DriveInfo In getInfo
            If info.IsReady Then
                newNode = New TreeNode(info.Name & info.VolumeLabel)
            Else
                newNode = New TreeNode(info.Name)
            End If
            newNode.ImageIndex = 0
            Dim di As DirectoryInfo = New DirectoryInfo(info.Name)
            newNode.Tag = di
            TreeView.Nodes.Add(newNode)
            nodetree(newNode)

        Next
    End Sub
    Public Function nodetree(ByVal sender As TreeNode)
        Try
            Dim longpath As String = Nothing
            If TypeOf sender.Tag Is DirectoryInfo Then
                Dim Sinfo As DirectoryInfo = sender.Tag
                longpath = Sinfo.FullName
            Else
                Dim Finfo As FileInfo = sender.Tag
                longpath = Finfo.DirectoryName
            End If

            Dim dirs() As String = Nothing
            Dim dirname As String = Nothing

            dirs = Directory.GetDirectories(longpath, "*.*", SearchOption.TopDirectoryOnly)

            For Each d As String In dirs
                Dim di As New DirectoryInfo(d)
                dirname = di.Name
                Dim newnode As New TreeNode(dirname)
                newnode.Tag = di
                sender.Nodes.Add(newnode)
            Next

        Catch
        End Try
        Return Nothing
    End Function

    Private Sub TreeView1_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView.BeforeExpand
        Try
            Dim Nnodes As TreeNodeCollection = e.Node.Nodes
            For Each N As TreeNode In Nnodes
                If TypeOf N.Tag Is DirectoryInfo Then
                    If N.Nodes.Count = 0 Then
                        nodetree(N)
                    End If
                End If
            Next
        Catch ex As Exception
        End Try
    End Sub

    Private Sub SetUpListViewColumns()

        ' TODO: Add code to set up listview columns
        Dim lvColumnHeader As ColumnHeader

        ' Setting Column widths applies only to the current view, so this line
        '  explicitly sets the ListView to be showing the SmallIcon view
        '  before setting the column width
        SetView(View.SmallIcon)

        lvColumnHeader = ListView.Columns.Add("Column1")
        ' Set the SmallIcon view column width large enough so that the items
        '  do not overlap
        ' Note that the second and third column do not appear in SmallIcon
        '  view, so there is no need to set those while in SmallIcon view
        lvColumnHeader.Width = 90

        ' Change the view to Details and set up the appropriate column
        '  widths for the Details view differently than SmallIcon view
        SetView(View.Details)

        ' The first column needs to be slightly larger in Details view than it
        '  was for SmallIcon view, and Column2 and Column3 need explicit sizes
        '  set for Details view
        lvColumnHeader.Width = 100

        lvColumnHeader = ListView.Columns.Add("Column2")
        lvColumnHeader.Width = 70

        lvColumnHeader = ListView.Columns.Add("Column3")
        lvColumnHeader.Width = 70

    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        'Close this form
        Me.Close()
    End Sub

    Private Sub ToolBarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolBarToolStripMenuItem.Click
        'Toggle the visibility of the toolstrip and also the checked state of the associated menu item
        ToolBarToolStripMenuItem.Checked = Not ToolBarToolStripMenuItem.Checked
        ToolStrip.Visible = ToolBarToolStripMenuItem.Checked
    End Sub

    Private Sub StatusBarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatusBarToolStripMenuItem.Click
        'Toggle the visibility of the statusstrip and also the checked state of the associated menu item
        StatusBarToolStripMenuItem.Checked = Not StatusBarToolStripMenuItem.Checked
        StatusStrip.Visible = StatusBarToolStripMenuItem.Checked
    End Sub

    'Change whether or not the folders pane is visible
    Private Sub ToggleFoldersVisible()
        'First toggle the checked state of the associated menu item
        FoldersToolStripMenuItem.Checked = Not FoldersToolStripMenuItem.Checked

        'Change the Folders toolbar button to be in sync
        FoldersToolStripButton.Checked = FoldersToolStripMenuItem.Checked

        ' Collapse the Panel containing the TreeView.
        Me.SplitContainer.Panel1Collapsed = Not FoldersToolStripMenuItem.Checked
    End Sub

    Private Sub FoldersToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FoldersToolStripMenuItem.Click
        ToggleFoldersVisible()
    End Sub

    Private Sub FoldersToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FoldersToolStripButton.Click
        ToggleFoldersVisible()
    End Sub

    Private Sub SetView(ByVal View As System.Windows.Forms.View)
        'Figure out which menu item should be checked
        Dim MenuItemToCheck As ToolStripMenuItem = Nothing
        Select Case View
            Case View.Details
                MenuItemToCheck = DetailsToolStripMenuItem
            Case View.LargeIcon
                MenuItemToCheck = LargeIconsToolStripMenuItem
            Case View.List
                MenuItemToCheck = ListToolStripMenuItem
            Case View.SmallIcon
                MenuItemToCheck = SmallIconsToolStripMenuItem
            Case View.Tile
                MenuItemToCheck = TileToolStripMenuItem
            Case Else
                Debug.Fail("Unexpected View")
                View = View.Details
                MenuItemToCheck = DetailsToolStripMenuItem
        End Select

        'Check the appropriate menu item and deselect all others under the Views menu
        For Each MenuItem As ToolStripMenuItem In ListViewToolStripButton.DropDownItems
            If MenuItem Is MenuItemToCheck Then
                MenuItem.Checked = True
            Else
                MenuItem.Checked = False
            End If
        Next

        'Finally, set the view requested
        ListView.View = View
    End Sub

    Private Sub ListToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListToolStripMenuItem.Click
        SetView(View.List)
    End Sub

    Private Sub DetailsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DetailsToolStripMenuItem.Click
        SetView(View.Details)
    End Sub

    Private Sub LargeIconsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LargeIconsToolStripMenuItem.Click
        SetView(View.LargeIcon)
    End Sub

    Private Sub SmallIconsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SmallIconsToolStripMenuItem.Click
        SetView(View.SmallIcon)
    End Sub

    Private Sub TileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TileToolStripMenuItem.Click
        SetView(View.Tile)
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim OpenFileDialog As New OpenFileDialog
        OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        OpenFileDialog.Filter = "Text Files (*.txt)|*.txt"
        OpenFileDialog.ShowDialog(Me)

        Dim FileName As String = OpenFileDialog.FileName
        ' TODO: Add code to open the file
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveAsToolStripMenuItem.Click
        Dim SaveFileDialog As New SaveFileDialog
        SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        SaveFileDialog.Filter = "Text Files (*.txt)|*.txt"
        SaveFileDialog.ShowDialog(Me)

        Dim FileName As String = SaveFileDialog.FileName
        ' TODO: Add code here to save the current contents of the form to a file.
    End Sub

    Private Sub TreeView_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView.AfterSelect
        ' TODO: Add code to change the listview contents based on the currently-selected node of the treeview
        ListView.Items.Clear()
        Dim dirs() As String = Nothing
        Dim dirname As String = Nothing
        Dim dinfo As DirectoryInfo
        dinfo = e.Node.Tag
        dirs = Directory.GetDirectories(dinfo.FullName, "*.*", SearchOption.TopDirectoryOnly)

        For Each d As String In dirs
            Dim di As New DirectoryInfo(d)
            Dim temp As ListViewItem = ListView.Items.Add(di.Name)
            temp.ImageIndex = 0
        Next

        Dim files() As String = Directory.GetFiles(dinfo.FullName, "*.*", SearchOption.TopDirectoryOnly)
        For Each f As String In files
            Dim fi As New FileInfo(f)
            Dim temp As ListViewItem = ListView.Items.Add(fi.Name)
            temp.ImageIndex = 1
        Next
    End Sub

End Class

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s