I'm trying to create a directory tree.
I only want to show the name of the directory and not the full pathname. Can someone tell me how to do so.
Below is my code
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TreeView1 = New System.Windows.Forms.TreeView
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TreeView1
'
Me.TreeView1.ImageIndex = -1
Me.TreeView1.Location = New System.Drawing.Point(8, 8)
Me.TreeView1.Name = "TreeView1"
Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("My Computer"})
Me.TreeView1.SelectedImageIndex = -1
Me.TreeView1.Size = New System.Drawing.Size(576, 184)
Me.TreeView1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(608, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(80, 40)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(704, 213)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TreeView1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Public blnload As Boolean = True
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ShowAllDrives()
End Sub
Sub ShowAllDrives()
Dim drives() As String
Dim cnode As TreeNode
cnode = TreeView1.Nodes(0)
drives = Directory.GetLogicalDrives()
Dim aDrive As String
Dim di As DirectoryInfo
For Each aDrive In drives
di = New DirectoryInfo(aDrive)
cnode.Nodes.Add(di.Name)
Next
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If TreeView1.SelectedNode Is Nothing Then Exit Sub
If TreeView1.SelectedNode Is TreeView1.Nodes(0) Then Exit Sub
Dim folders() As String
Dim cnode As TreeNode
cnode = TreeView1.SelectedNode
folders = Directory.GetDirectories(TreeView1.SelectedNode.Text)
Dim f As String
For Each f In folders
cnode.Nodes.Add(f)
Next
TreeView1.SelectedNode.Expand()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myFolder As String
myFolder = TreeView1.SelectedNode.Text
MsgBox(myFolder)
End Sub
End Class
Live fast, die young and leave a beautiful corpse behind.
I only want to show the name of the directory and not the full pathname. Can someone tell me how to do so.
Below is my code
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TreeView1 = New System.Windows.Forms.TreeView
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TreeView1
'
Me.TreeView1.ImageIndex = -1
Me.TreeView1.Location = New System.Drawing.Point(8, 8)
Me.TreeView1.Name = "TreeView1"
Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("My Computer"})
Me.TreeView1.SelectedImageIndex = -1
Me.TreeView1.Size = New System.Drawing.Size(576, 184)
Me.TreeView1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(608, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(80, 40)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(704, 213)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TreeView1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Public blnload As Boolean = True
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ShowAllDrives()
End Sub
Sub ShowAllDrives()
Dim drives() As String
Dim cnode As TreeNode
cnode = TreeView1.Nodes(0)
drives = Directory.GetLogicalDrives()
Dim aDrive As String
Dim di As DirectoryInfo
For Each aDrive In drives
di = New DirectoryInfo(aDrive)
cnode.Nodes.Add(di.Name)
Next
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If TreeView1.SelectedNode Is Nothing Then Exit Sub
If TreeView1.SelectedNode Is TreeView1.Nodes(0) Then Exit Sub
Dim folders() As String
Dim cnode As TreeNode
cnode = TreeView1.SelectedNode
folders = Directory.GetDirectories(TreeView1.SelectedNode.Text)
Dim f As String
For Each f In folders
cnode.Nodes.Add(f)
Next
TreeView1.SelectedNode.Expand()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myFolder As String
myFolder = TreeView1.SelectedNode.Text
MsgBox(myFolder)
End Sub
End Class
Live fast, die young and leave a beautiful corpse behind.