g00r00
asked on
Browsing folder in vb.net
I want to be able browse for folders in my application, not start different dialog but right on my main menu. Should i use some kind of label or something? Can i be able to browse for folders & for files, work with folders & files without starting explorer.exe ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Below is a VERY BASIC explorer like interface that I wrote. It is not very effecient, or pretty for that manner, but it is fairly easy to understand how it works.
The project here http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2224&lngWId=10 is very good and worth the download.
Regards,
Idle_Mind
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.ICon tainer
'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 directoryTree As System.Windows.Forms.TreeV iew
Friend WithEvents curDir As System.Windows.Forms.Label
Friend WithEvents filesListView As System.Windows.Forms.ListV iew
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.directoryTree = New System.Windows.Forms.TreeV iew
Me.curDir = New System.Windows.Forms.Label
Me.filesListView = New System.Windows.Forms.ListV iew
Me.SuspendLayout()
'
'directoryTree
'
Me.directoryTree.Anchor = CType(((System.Windows.For ms.AnchorS tyles.Top Or System.Windows.Forms.Ancho rStyles.Bo ttom) _
Or System.Windows.Forms.Ancho rStyles.Le ft), System.Windows.Forms.Ancho rStyles)
Me.directoryTree.ImageInde x = -1
Me.directoryTree.Location = New System.Drawing.Point(8, 32)
Me.directoryTree.Name = "directoryTree"
Me.directoryTree.SelectedI mageIndex = -1
Me.directoryTree.Size = New System.Drawing.Size(224, 360)
Me.directoryTree.TabIndex = 0
'
'curDir
'
Me.curDir.Location = New System.Drawing.Point(16, 8)
Me.curDir.Name = "curDir"
Me.curDir.Size = New System.Drawing.Size(560, 16)
Me.curDir.TabIndex = 1
'
'filesListView
'
Me.filesListView.Anchor = CType((((System.Windows.Fo rms.Anchor Styles.Top Or System.Windows.Forms.Ancho rStyles.Bo ttom) _
Or System.Windows.Forms.Ancho rStyles.Le ft) _
Or System.Windows.Forms.Ancho rStyles.Ri ght), System.Windows.Forms.Ancho rStyles)
Me.filesListView.Location = New System.Drawing.Point(240, 32)
Me.filesListView.Name = "filesListView"
Me.filesListView.Size = New System.Drawing.Size(336, 360)
Me.filesListView.TabIndex = 2
Me.filesListView.View = System.Windows.Forms.View. List
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(584, 398)
Me.Controls.Add(Me.filesLi stView)
Me.Controls.Add(Me.curDir)
Me.Controls.Add(Me.directo ryTree)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Cursor.Current = Cursors.WaitCursor
Dim drives() As String = Directory.GetLogicalDrives ()
Dim drive As String
Dim rootDrive As TreeNode
directoryTree.BeginUpdate( )
For Each drive In drives
rootDrive = New TreeNode(drive)
directoryTree.Nodes.Add(ro otDrive)
getSubDirs(rootDrive, rootDrive.Text, 0)
Next drive
directoryTree.EndUpdate()
Cursor.Current = Cursors.Default
End Sub
Private Sub getSubDirs(ByVal parentDir As TreeNode, ByVal fullPath As String, ByVal recurseLevel As Integer)
Try
parentDir.Nodes.Clear()
Dim subDirs() As String = Directory.GetDirectories(f ullPath, "*")
Dim subDir As String
Dim subDirNode As TreeNode
Dim di As DirectoryInfo
recurseLevel = recurseLevel + 1
If recurseLevel <= 2 Then
For Each subDir In subDirs
di = New DirectoryInfo(subDir)
If Not ((di.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden) Then
subDirNode = New TreeNode(subDir.Substring( subDir.Las tIndexOf(" \") + 1))
parentDir.Nodes.Add(subDir Node)
getSubDirs(subDirNode, subDir & "\", recurseLevel)
End If
Next subDir
End If
Catch ex As Exception
Debug.WriteLine(ex.Message )
End Try
End Sub
Private Sub directoryTree_BeforeExpand (ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeV iewCancelE ventArgs) Handles directoryTree.BeforeExpand
Dim fp As String = getFullPath(e.Node)
Cursor.Current = Cursors.WaitCursor
getSubDirs(e.Node, fp, 0)
Cursor.Current = Cursors.Default
End Sub
Private Function getFullPath(ByVal n As TreeNode) As String
Dim fp As String = n.FullPath
fp = fp.Replace("\\", "\")
If Not fp.EndsWith("\") Then
fp = fp & "\"
End If
Return fp
End Function
Private Sub directoryTree_AfterSelect( ByVal sender As Object, ByVal e As System.Windows.Forms.TreeV iewEventAr gs) Handles directoryTree.AfterSelect
curDir.Text = getFullPath(directoryTree. SelectedNo de)
updateFolderContents(curDi r.Text)
End Sub
Private Sub updateFolderContents(ByVal path As String)
Try
Dim di As New DirectoryInfo(path)
Dim fi As FileInfo() = di.GetFiles()
Dim fiTemp As FileInfo
Dim fileEntry As ListViewItem
filesListView.Items.Clear( )
For Each fiTemp In fi
If Not ((fiTemp.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden) Then
fileEntry = New ListViewItem(fiTemp.Name)
filesListView.Items.Add(fi leEntry)
End If
Next fiTemp
Catch ex As Exception
Debug.WriteLine(ex.Message )
End Try
End Sub
End Class
The project here http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=2224&lngWId=10 is very good and worth the download.
Regards,
Idle_Mind
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.ICon
'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 directoryTree As System.Windows.Forms.TreeV
Friend WithEvents curDir As System.Windows.Forms.Label
Friend WithEvents filesListView As System.Windows.Forms.ListV
<System.Diagnostics.Debugg
Me.directoryTree = New System.Windows.Forms.TreeV
Me.curDir = New System.Windows.Forms.Label
Me.filesListView = New System.Windows.Forms.ListV
Me.SuspendLayout()
'
'directoryTree
'
Me.directoryTree.Anchor = CType(((System.Windows.For
Or System.Windows.Forms.Ancho
Me.directoryTree.ImageInde
Me.directoryTree.Location = New System.Drawing.Point(8, 32)
Me.directoryTree.Name = "directoryTree"
Me.directoryTree.SelectedI
Me.directoryTree.Size = New System.Drawing.Size(224, 360)
Me.directoryTree.TabIndex = 0
'
'curDir
'
Me.curDir.Location = New System.Drawing.Point(16, 8)
Me.curDir.Name = "curDir"
Me.curDir.Size = New System.Drawing.Size(560, 16)
Me.curDir.TabIndex = 1
'
'filesListView
'
Me.filesListView.Anchor = CType((((System.Windows.Fo
Or System.Windows.Forms.Ancho
Or System.Windows.Forms.Ancho
Me.filesListView.Location = New System.Drawing.Point(240, 32)
Me.filesListView.Name = "filesListView"
Me.filesListView.Size = New System.Drawing.Size(336, 360)
Me.filesListView.TabIndex = 2
Me.filesListView.View = System.Windows.Forms.View.
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(584, 398)
Me.Controls.Add(Me.filesLi
Me.Controls.Add(Me.curDir)
Me.Controls.Add(Me.directo
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Cursor.Current = Cursors.WaitCursor
Dim drives() As String = Directory.GetLogicalDrives
Dim drive As String
Dim rootDrive As TreeNode
directoryTree.BeginUpdate(
For Each drive In drives
rootDrive = New TreeNode(drive)
directoryTree.Nodes.Add(ro
getSubDirs(rootDrive, rootDrive.Text, 0)
Next drive
directoryTree.EndUpdate()
Cursor.Current = Cursors.Default
End Sub
Private Sub getSubDirs(ByVal parentDir As TreeNode, ByVal fullPath As String, ByVal recurseLevel As Integer)
Try
parentDir.Nodes.Clear()
Dim subDirs() As String = Directory.GetDirectories(f
Dim subDir As String
Dim subDirNode As TreeNode
Dim di As DirectoryInfo
recurseLevel = recurseLevel + 1
If recurseLevel <= 2 Then
For Each subDir In subDirs
di = New DirectoryInfo(subDir)
If Not ((di.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden) Then
subDirNode = New TreeNode(subDir.Substring(
parentDir.Nodes.Add(subDir
getSubDirs(subDirNode, subDir & "\", recurseLevel)
End If
Next subDir
End If
Catch ex As Exception
Debug.WriteLine(ex.Message
End Try
End Sub
Private Sub directoryTree_BeforeExpand
Dim fp As String = getFullPath(e.Node)
Cursor.Current = Cursors.WaitCursor
getSubDirs(e.Node, fp, 0)
Cursor.Current = Cursors.Default
End Sub
Private Function getFullPath(ByVal n As TreeNode) As String
Dim fp As String = n.FullPath
fp = fp.Replace("\\", "\")
If Not fp.EndsWith("\") Then
fp = fp & "\"
End If
Return fp
End Function
Private Sub directoryTree_AfterSelect(
curDir.Text = getFullPath(directoryTree.
updateFolderContents(curDi
End Sub
Private Sub updateFolderContents(ByVal
Try
Dim di As New DirectoryInfo(path)
Dim fi As FileInfo() = di.GetFiles()
Dim fiTemp As FileInfo
Dim fileEntry As ListViewItem
filesListView.Items.Clear(
For Each fiTemp In fi
If Not ((fiTemp.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden) Then
fileEntry = New ListViewItem(fiTemp.Name)
filesListView.Items.Add(fi
End If
Next fiTemp
Catch ex As Exception
Debug.WriteLine(ex.Message
End Try
End Sub
End Class
...or...
Do you want to use a treeview (like in explorer)?
Idle_Mind