You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

568 lines
22 KiB

Imports System.IO
Imports System.Windows.Forms
' Folder Select
' An VB.NET RC1 Windows Application converted from
' An C#.NET beta 2 example on how to build a folder browser dialog window
' The Code Project - Folder Select Dialog By Chris Warner
' http://www.codeproject.com/csharp/folderseldlg.asp
'
' Created & improved by Goh Mingkun
' mangokun@hotmail.com
' Saturday, February 02, 2002
'
' Add this Windows Form(folderSelect.vb) and
' its Resource file(folderSelect.resx) to your project,
' then follow the method below to use it.
'
' <Usage>
'
' Dim dlg As FolderSelect = New FolderSelect()
'
' ' Directory Tree display options
' dlg.InitialDirectory = "C:\Program Files" ' set the initial directory to be selected
' dlg.InitialDirectoryExpanded = True ' expand the selected directory
' dlg.ShowHiddenDirectory = True ' display hidden directories
' dlg.ShowSystemDirectory = True ' display system directories
'
' If dlg.ShowDialog() = DialogResult.OK Then
' txtSelectedFolder.Text = dlg.fullPath ' retrieve the selected folder
'
' Dim info As DirectoryInfo = dlg.info ' retrieve the selected folder's info
' ' extract the directory info.
' Dim strArray(4) As String
'
' strArray(0) = "Folder Name : " + dlg.foldername
' strArray(1) = "Full Path : " + info.FullName
' strArray(2) = "Creation Time : " + info.CreationTime.ToString()
' strArray(3) = "Last Access Time : " + info.LastAccessTime.ToString()
' strArray(4) = "Last Write Time : " + info.LastWriteTime.ToString()
'
' txtInfo.Lines = strArray
' End If
'
' </Usage>
Public Class FolderSelect
Inherits System.Windows.Forms.Form
Private driveLetters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Private folder As DirectoryInfo
Private SelectedNodeAlreadyExpanded As Boolean
Private SelectedNodeAlreadyCollapsed As Boolean
' This method is used to initially fill the treeView control with a list
' of available drives from which you can search for the desired folder.
Private Sub fillTree()
Dim directory As DirectoryInfo
Dim sCurPath As String = ""
' clear out the old values
TreeView1.Nodes.Clear()
Dim c As Char
' loop through the drive letters and find the available drives.
For Each c In driveLetters
sCurPath = c + ":\"
Try
' get the directory informaiton for this path.
directory = New DirectoryInfo(sCurPath)
' if the retrieved directory information points to a valid
' directory or drive in this case, add it to the root of the
' treeView.
If directory.Exists Then
'MsgBox(sCurPath & " directory.Exists")
Dim newNode As TreeNode = New TreeNode(directory.FullName)
TreeView1.Nodes.Add(newNode) ' add the new node to the root level.
' NEW TEEEEEEST
If sInitialDirectory = sCurPath Then
' NEW TEEEEEEST
getSubDirs(newNode) ' scan for any sub folders on this drive.
' NEW TEEEEEEST
End If
' NEW TEEEEEEST
End If
Catch doh As Exception
Console.WriteLine(doh.Message)
End Try
Next
End Sub
' method getSubDirs
' this function will scan the specified parent for any subfolders
' if they exist. To minimize the memory usage, we only scan a single
' folder level down from the existing, and only if it is needed.
' the parent folder in which to search for sub-folders.
Private Sub getSubDirs(ByVal parent As TreeNode)
Dim directory As DirectoryInfo
Try
' if we have not scanned this folder before
If parent.Nodes.Count = 0 Then
directory = New DirectoryInfo(parent.FullPath)
Dim dir As DirectoryInfo
Dim IsHidden, IsSystem As Boolean
Dim Skip As Boolean
For Each dir In directory.GetDirectories()
' Determine whether to show directory
IsHidden = False
IsSystem = False
Skip = False
'MsgBox(dir.FullName)
If (GetAttr(dir.FullName) And FileAttribute.Hidden) = FileAttributes.Hidden Then
'MsgBox(dir.FullName & " is a hidden directory")
IsHidden = True
Skip = True
End If
If (GetAttr(dir.FullName) And FileAttribute.System) = FileAttributes.System Then
'MsgBox(dir.FullName & " is a system directory")
IsSystem = True
Skip = True
End If
If IsHidden And bShowHiddenDirectory Then
Skip = False
End If
If IsSystem And bShowSystemDirectory Then
Skip = False
End If
If Not Skip Then
Dim newNode As TreeNode = New TreeNode(dir.Name)
parent.Nodes.Add(newNode)
End If
Next
End If
' now that we have the children of the parent, see if they
' have any child members that need to be scanned. Scanning
' the first level of sub folders insures that you properly
' see the '+' or '-' expanding controls on each node that represents
' a sub folder with it's own children.
Dim node As TreeNode
For Each node In parent.Nodes
'MsgBox(node.Text & " has " & node.Nodes.Count)
' if we have not scanned this node before.
If node.Nodes.Count = 0 Then
' get the folder information for the specified path.
directory = New DirectoryInfo(node.FullPath)
' check this folder for any possible sub-folders
Dim dir As DirectoryInfo
Try
For Each dir In directory.GetDirectories()
' make a new TreeNode and add it to the treeView.
Dim newNode As TreeNode = New TreeNode(dir.Name)
node.Nodes.Add(newNode)
Next
Catch
End Try
End If
Next
Catch doh As Exception
Console.WriteLine(doh.Message)
End Try
End Sub
' method fixPath
' For some reason, the treeView will only work with paths constructed like the following example.
' "c:\\Program Files\Microsoft\...". What this method does is strip the leading "\\" next to the drive
' letter.
' the folder that needs it's path fixed for display.
' The correctly formatted full path to the selected folder.
Private Function fixPath(ByVal node As TreeNode) As String
Dim sRet As String = ""
Try
sRet = node.FullPath
Dim index As Integer = sRet.IndexOf("\\")
If index > 1 Then
sRet = node.FullPath.Remove(index, 1)
End If
Catch doh As Exception
Console.WriteLine(doh.Message)
End Try
Return sRet
End Function
#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
' initialize the treeView
'fillTree()
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 Panel2 As System.Windows.Forms.Panel
Friend WithEvents Panel3 As System.Windows.Forms.Panel
Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
Friend WithEvents btnSelect As System.Windows.Forms.Button
Friend WithEvents btnCancel As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(FolderSelect))
Me.Panel2 = New System.Windows.Forms.Panel()
Me.btnSelect = New System.Windows.Forms.Button()
Me.btnCancel = New System.Windows.Forms.Button()
Me.Panel3 = New System.Windows.Forms.Panel()
Me.TreeView1 = New System.Windows.Forms.TreeView()
Me.ImageList1 = New System.Windows.Forms.ImageList(Me.components)
Me.Panel2.SuspendLayout()
Me.Panel3.SuspendLayout()
Me.SuspendLayout()
'
'Panel2
'
Me.Panel2.AutoScroll = True
Me.Panel2.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSelect, Me.btnCancel})
Me.Panel2.Dock = System.Windows.Forms.DockStyle.Bottom
Me.Panel2.DockPadding.All = 10
Me.Panel2.Location = New System.Drawing.Point(6, 222)
Me.Panel2.Name = "Panel2"
Me.Panel2.Size = New System.Drawing.Size(280, 45)
Me.Panel2.TabIndex = 1
'
'btnSelect
'
Me.btnSelect.DialogResult = System.Windows.Forms.DialogResult.OK
Me.btnSelect.Dock = System.Windows.Forms.DockStyle.Left
Me.btnSelect.Location = New System.Drawing.Point(10, 10)
Me.btnSelect.Name = "btnSelect"
Me.btnSelect.Size = New System.Drawing.Size(75, 25)
Me.btnSelect.TabIndex = 1
Me.btnSelect.Text = "&OK"
'
'btnCancel
'
Me.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
Me.btnCancel.Dock = System.Windows.Forms.DockStyle.Right
Me.btnCancel.Location = New System.Drawing.Point(198, 10)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.Size = New System.Drawing.Size(72, 25)
Me.btnCancel.TabIndex = 2
Me.btnCancel.Text = "&Cancel"
'
'Panel3
'
Me.Panel3.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.Panel3.Controls.AddRange(New System.Windows.Forms.Control() {Me.TreeView1})
Me.Panel3.DockPadding.All = 10
Me.Panel3.Location = New System.Drawing.Point(6, 0)
Me.Panel3.Name = "Panel3"
Me.Panel3.Size = New System.Drawing.Size(280, 224)
Me.Panel3.TabIndex = 5
'
'TreeView1
'
Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Fill
Me.TreeView1.ImageList = Me.ImageList1
Me.TreeView1.ImeMode = System.Windows.Forms.ImeMode.NoControl
Me.TreeView1.Location = New System.Drawing.Point(10, 10)
Me.TreeView1.Name = "TreeView1"
Me.TreeView1.Size = New System.Drawing.Size(260, 204)
Me.TreeView1.TabIndex = 0
'
'ImageList1
'
Me.ImageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit
Me.ImageList1.ImageSize = New System.Drawing.Size(16, 16)
Me.ImageList1.ImageStream = CType(resources.GetObject("ImageList1.ImageStream"), System.Windows.Forms.ImageListStreamer)
Me.ImageList1.TransparentColor = System.Drawing.Color.Transparent
'
'FolderSelect
'
Me.AcceptButton = Me.btnSelect
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.CancelButton = Me.btnCancel
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Panel3, Me.Panel2})
Me.DockPadding.All = 6
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.KeyPreview = True
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.MinimumSize = New System.Drawing.Size(230, 300)
Me.Name = "FolderSelect"
Me.ShowInTaskbar = False
Me.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
Me.Text = "Verzeichnis ausw<73>hlen"
Me.Panel2.ResumeLayout(False)
Me.Panel3.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
' method treeView1_BeforeSelect
' Before we select a tree node we want to make sure that we scan the soon to be selected
' tree node for any sub-folders. this insures proper tree construction on the fly.
' The object that invoked this event
' The TreeViewCancelEventArgs event arguments.
Private Sub TreeView1_BeforeSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeSelect
getSubDirs(e.Node) ' get the sub-folders for the selected node.
' TextBox1.Text = fixPath(e.Node) ' update the user selection text box.
folder = New DirectoryInfo(e.Node.FullPath) ' get it's Directory info.
' change the image of the selected node to a selected folder icon
If e.Node.IsExpanded Then
e.Node.ImageIndex = 1
e.Node.SelectedImageIndex = 3
Else
e.Node.ImageIndex = 0
e.Node.SelectedImageIndex = 2
End If
End Sub
' method treeView1_BeforeExpand
' Before we expand a tree node we want to make sure that we scan the soon to be expanded
' tree node for any sub-folders. this insures proper tree construction on the fly.
' The object that invoked this event
' The TreeViewCancelEventArgs event arguments.
Private Sub TreeView1_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
If e.Node Is TreeView1.SelectedNode Then
If SelectedNodeAlreadyCollapsed Then
e.Cancel = True
Exit Sub
End If
End If
getSubDirs(e.Node) ' get the sub-folders for the selected node.
'TextBox1.Text = fixPath(e.Node) ' update the user selection text box.
'folder = New DirectoryInfo(e.Node.FullPath) ' get it's Directory info.
' change the image of the expanded node to an opened folder icon
e.Node.ImageIndex = 1
e.Node.SelectedImageIndex = 3
End Sub
Private Sub TreeView1_BeforeCollapse(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeCollapse
If e.Node Is TreeView1.SelectedNode Then
If SelectedNodeAlreadyExpanded Then
e.Cancel = True
Exit Sub
End If
End If
' change the image of the collapsed node back to a closed folder icon
e.Node.ImageIndex = 0
e.Node.SelectedImageIndex = 2
End Sub
' method cancelBtn_Click
' This method cancels the folder browsing.
Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
folder = Nothing
Me.Close()
End Sub
' method selectBtn_Click
' This method accepts which ever folder is selected and closes this application
' with a DialogResult.OK result if you invoke this form though Form.ShowDialog().
Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
Me.Close()
End Sub
Private Sub FolderSelect_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
' initialize the treeView
fillTree()
Me.Activate()
TreeView1.Select()
If sInitialDirectory <> "" Then
TreeView1.SelectedNode = GetInitialNode()
End If
If bInitialDirectoryExpanded Then
TreeView1.SelectedNode.Expand()
End If
Catch doh As Exception
Console.WriteLine(doh.Message)
End Try
End Sub
Private Function GetInitialNode() As TreeNode
Dim path As String = sInitialDirectory
If Not path.EndsWith("\") Then
path += "\"
End If
Dim node, subnode As TreeNode
Dim index As Integer
Dim label As String
' Get the drive
index = path.IndexOf("\")
label = path.Substring(0, index + 1)
' Search the nodes for the drive
For Each node In TreeView1.Nodes
If node.Text.ToLower = label.ToLower Then
path = path.Remove(0, index + 1)
Do While path <> ""
node.Expand()
'MsgBox(path)
index = path.IndexOf("\")
label = path.Substring(0, index)
'MsgBox(label)
' Search the nodes for the sub-directory
For Each subnode In node.Nodes
If subnode.Text.ToLower = label.ToLower Then
node = subnode
Exit For
End If
Next
path = path.Remove(0, index + 1)
Loop
Exit For
End If
Next
Return node
End Function
' handle key events
Private Sub FolderSelect_KeyDown(ByVal sender As System.Object, ByVal e As KeyEventArgs) Handles MyBase.KeyDown
'MsgBox(e.KeyCode)
Select Case e.KeyCode
Case Keys.Add
'MsgBox("Add")
SelectedNodeAlreadyExpanded = TreeView1.SelectedNode.IsExpanded
SelectedNodeAlreadyCollapsed = False
Case Keys.KeyCode.Subtract
'MsgBox("Subtract")
SelectedNodeAlreadyExpanded = False
SelectedNodeAlreadyCollapsed = Not TreeView1.SelectedNode.IsExpanded
Case Keys.Left
SelectedNodeAlreadyExpanded = False
End Select
End Sub
' method foldername
' A method to retrieve the name for the selected folder,
' without the full path.
' This method will return empty string
' when root of drive is selected
Public ReadOnly Property foldername() As String
Get
If Not IsNothing(folder) And folder.Exists Then
Dim FullPath As String = fixPath(TreeView1.SelectedNode)
Dim index As Integer = FullPath.LastIndexOf("\")
Return FullPath.Substring(index + 1)
Else
Return Nothing
End If
End Get
End Property
' method fullPath
' Retrieve the full path for the selected folder.
' The correctly formatted full path to the selected folder.
Public ReadOnly Property fullPath() As String
Get
If Not IsNothing(folder) And folder.Exists And Not IsNothing(TreeView1.SelectedNode) Then
Return fixPath(TreeView1.SelectedNode)
Else
Return Nothing
End If
End Get
End Property
' method info
' Retrieve the full DirectoryInfo object associated with the selected folder.
' Note that this will not have the corrected full path string.
' The full DirectoryInfo object associated with the selected folder.
Public ReadOnly Property info() As DirectoryInfo
Get
If Not IsNothing(folder) And folder.Exists Then
Return folder
Else
Return Nothing
End If
End Get
End Property
' Property InitialDirectory
' Set InitialDirectory to be selected and visible
Private sInitialDirectory As String
Public Property InitialDirectory() As String
Get
Return sInitialDirectory
End Get
Set(ByVal Value As String)
If Directory.Exists(Value) Then
sInitialDirectory = Value.Trim
End If
End Set
End Property
' Property InitialDirectory
' Determine whether InitialDirectory is to be expanded when displayed
Private bInitialDirectoryExpanded As Boolean
Public Property InitialDirectoryExpanded() As Boolean
Get
Return bInitialDirectoryExpanded
End Get
Set(ByVal Value As Boolean)
bInitialDirectoryExpanded = Value
End Set
End Property
' Property ShowHiddenDirectory
' Determine whether hidden directories are displayed
Private bShowHiddenDirectory As Boolean
Public Property ShowHiddenDirectory() As Boolean
Get
Return bShowHiddenDirectory
End Get
Set(ByVal Value As Boolean)
bShowHiddenDirectory = Value
End Set
End Property
' Property ShowSystemDirectory
' Determine whether system directories are displayed
Private bShowSystemDirectory As Boolean
Public Property ShowSystemDirectory() As Boolean
Get
Return bShowSystemDirectory
End Get
Set(ByVal Value As Boolean)
bShowSystemDirectory = Value
End Set
End Property
End Class