⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customexplorerform.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:
Imports System.IO
Public Class CustomExplorer
    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
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
    Friend WithEvents ListView1 As System.Windows.Forms.ListView
    Friend WithEvents FileColumn As System.Windows.Forms.ColumnHeader
    Friend WithEvents CreatedColumn As System.Windows.Forms.ColumnHeader
    Friend WithEvents AccessedColumn As System.Windows.Forms.ColumnHeader
    Friend WithEvents SizeColumn As System.Windows.Forms.ColumnHeader

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    '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.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TreeView1 = New System.Windows.Forms.TreeView()
        Me.Splitter1 = New System.Windows.Forms.Splitter()
        Me.ListView1 = New System.Windows.Forms.ListView()
        Me.FileColumn = New System.Windows.Forms.ColumnHeader()
        Me.SizeColumn = New System.Windows.Forms.ColumnHeader()
        Me.CreatedColumn = New System.Windows.Forms.ColumnHeader()
        Me.AccessedColumn = New System.Windows.Forms.ColumnHeader()
        Me.SuspendLayout()
        '
        'TreeView1
        '
        Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Left
        Me.TreeView1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.TreeView1.ImageIndex = -1
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.SelectedImageIndex = -1
        Me.TreeView1.Size = New System.Drawing.Size(330, 277)
        Me.TreeView1.TabIndex = 0
        '
        'Splitter1
        '
        Me.Splitter1.Location = New System.Drawing.Point(330, 0)
        Me.Splitter1.Name = "Splitter1"
        Me.Splitter1.Size = New System.Drawing.Size(4, 277)
        Me.Splitter1.TabIndex = 1
        Me.Splitter1.TabStop = False
        '
        'ListView1
        '
        Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.FileColumn, Me.SizeColumn, Me.CreatedColumn, Me.AccessedColumn})
        Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.ListView1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.ListView1.Location = New System.Drawing.Point(334, 0)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(346, 277)
        Me.ListView1.TabIndex = 2
        Me.ListView1.View = System.Windows.Forms.View.Details
        '
        'FileColumn
        '
        Me.FileColumn.Text = "File Name"
        Me.FileColumn.Width = 160
        '
        'SizeColumn
        '
        Me.SizeColumn.Text = "Size"
        Me.SizeColumn.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
        '
        'CreatedColumn
        '
        Me.CreatedColumn.Text = "Created"
        '
        'AccessedColumn
        '
        Me.AccessedColumn.Text = "Last Access"
        '
        'CustomExplorer
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(680, 277)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListView1, Me.Splitter1, Me.TreeView1})
        Me.Name = "CustomExplorer"
        Me.Text = "Custom File Explorer"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Nd As New TreeNode()
        ' change the name of the folder to be scanned in the following statement
        Dim initFolder As String = "C:\Program Files"
        Nd = TreeView1.Nodes.Add(initFolder)
        ' Only the statement that calls the ScanFolder() subroutine is required to populate the TreeView control
        ' Because quite a bit of code is executed from within the application's Load event handler,
        ' it will take a while before the form is displayed.
        ' Instead of waiting to populate the control before showing the form, you can call the Show
        ' method to show the form. In the ScanFolder() subroutine you must call the form's Refresh method 
        ' every now and then to update the form.
        Me.Show()
        Application.DoEvents()
        Me.Cursor = Cursors.WaitCursor
        ScanFolder(initFolder, Nd)
        Me.Cursor = Cursors.Default
    End Sub

    Sub ScanFolder(ByVal folderSpec As String, ByRef currentNode As TreeNode)
        Dim thisFolder As String
        Dim allFolders() As String

        allFolders = Directory.GetDirectories(folderSpec)
        For Each thisFolder In allFolders
            Dim Nd As TreeNode
            Nd = New TreeNode(thisFolder)
            currentNode.Nodes.Add(Nd)
            folderSpec = thisFolder
            ScanFolder(folderSpec, Nd)
            ' Call the Refresh method to update the display.
            ' Because it may take a while to populate the TreeView control with the structure
            ' of a folder with many subfolders/files, we update the display so that the user
            ' doesn't think the application isn't responding.
            ' An alternative would be to display the name of the folder being scanned
            ' on the form's caption bar with the following statement:
            '
            ' Me.Text = "Scanning " & folderSpec
            '
            ' When done, restore the form's caption to its original value

            Me.Refresh()
        Next
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Dim Nd As TreeNode
        Dim pathName As String
        Nd = TreeView1.SelectedNode
        pathName = Nd.Text
        ShowFiles(pathName)
    End Sub

    Sub ShowFiles(ByVal selFolder As String)
        ListView1.Items.Clear()
        Dim files() As String
        Dim file As String
        files = Directory.GetFiles(selFolder)
        For Each file In files
            Dim LItem As New ListViewItem()
            LItem.Text = ExtractFileName(file)
            Dim FI As New FileInfo(file)
            LItem.SubItems.Add(FI.Length.ToString("#,###"))
            LItem.SubItems.Add(FormatDateTime(Directory.GetCreationTime(file), DateFormat.ShortDate))
            LItem.SubItems.Add(FormatDateTime(Directory.GetLastAccessTime(file), DateFormat.ShortDate))
            ListView1.Items.Add(LItem)
        Next
    End Sub

    Function ExtractFileName(ByVal path As String) As String
        Return System.IO.Path.GetFileName(path)
        'Dim slashPos As Integer = path.LastIndexOf("\")
        'Return (path.Substring(slashPos + 1))
    End Function
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -