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

📄 foldermap.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:
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.

        Private WithEvents ToolTip1 As System.Windows.Forms.ToolTip
    Private WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
    Private WithEvents bttnMapFolder As System.Windows.Forms.Button
    Private WithEvents DrivesList As System.Windows.Forms.ComboBox
    Private WithEvents FoldersList As System.Windows.Forms.ListBox
        Private components As System.ComponentModel.IContainer

    'Required by the Windows Form Designer

    '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.components = New System.ComponentModel.Container()
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.DrivesList = New System.Windows.Forms.ComboBox()
        Me.RichTextBox1 = New System.Windows.Forms.RichTextBox()
        Me.bttnMapFolder = New System.Windows.Forms.Button()
        Me.FoldersList = New System.Windows.Forms.ListBox()
        Me.SuspendLayout()
        '
        'ToolTip1
        '
        Me.ToolTip1.ShowAlways = True
        '
        'DrivesList
        '
        Me.DrivesList.DropDownWidth = 160
        Me.DrivesList.Font = New System.Drawing.Font("Georgia", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.DrivesList.Location = New System.Drawing.Point(8, 8)
        Me.DrivesList.Name = "DrivesList"
        Me.DrivesList.Size = New System.Drawing.Size(160, 23)
        Me.DrivesList.TabIndex = 0
        Me.DrivesList.Text = "ComboBox1"
        '
        'RichTextBox1
        '
        Me.RichTextBox1.Location = New System.Drawing.Point(8, 160)
        Me.RichTextBox1.Name = "RichTextBox1"
        Me.RichTextBox1.Size = New System.Drawing.Size(608, 304)
        Me.RichTextBox1.TabIndex = 4
        Me.RichTextBox1.Text = ""
        '
        'bttnMapFolder
        '
        Me.bttnMapFolder.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.bttnMapFolder.Location = New System.Drawing.Point(432, 120)
        Me.bttnMapFolder.Name = "bttnMapFolder"
        Me.bttnMapFolder.Size = New System.Drawing.Size(184, 32)
        Me.bttnMapFolder.TabIndex = 3
        Me.bttnMapFolder.Text = "Map Selected Folder"
        '
        'FoldersList
        '
        Me.FoldersList.Font = New System.Drawing.Font("Georgia", 9!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FoldersList.ItemHeight = 15
        Me.FoldersList.Location = New System.Drawing.Point(8, 32)
        Me.FoldersList.Name = "FoldersList"
        Me.FoldersList.Size = New System.Drawing.Size(416, 124)
        Me.FoldersList.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(624, 469)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.RichTextBox1, Me.bttnMapFolder, Me.FoldersList, Me.DrivesList})
        Me.Name = "Form1"
        Me.Text = "FileScan"
        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 drives() As String
        drives = System.IO.Directory.GetLogicalDrives
        Dim iDrive As Integer
        DrivesList.Items.Clear()
        On Error Resume Next
        For iDrive = 0 To drives.GetUpperBound(0)
            DrivesList.Items.Add(drives(iDrive))
        Next
        DrivesList.SelectedIndex = 1
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrivesList.SelectedIndexChanged
        Dim directories() As String
        Try
            directories = System.IO.Directory.GetDirectories(DrivesList.Text)
        Catch drvException As Exception
            MsgBox(drvException.Message)
            Exit Sub
        End Try
        Dim dir As String
        FoldersList.Items.Clear()
        RichTextBox1.Clear()
        For Each dir In directories
            FoldersList.Items.Add(dir)
        Next
        Dim file As String
    End Sub

    Dim parentDir As String
    Dim boldFont As New Font("Verdana", 11, System.Drawing.FontStyle.Bold)
    Dim textFont As New Font("Verdana", 9, System.Drawing.FontStyle.Regular)

    Dim countFiles, countFolders As Integer

    Private Sub bttnMapFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnMapFolder.Click
        RichTextBox1.Clear()
        Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
        countFiles = 0
        countFolders = 1
        Try
            Scanfolder(FoldersList.SelectedItem)
        Catch scanException As Exception
            MsgBox(scanException.Message)
        End Try
        Me.Cursor = System.Windows.Forms.Cursors.Default
    End Sub

    Sub Scanfolder(ByVal currDir As String)
        Dim Dir As String
        Dim File As String
        RichTextBox1.SelectionFont = boldFont
        RichTextBox1.AppendText(currDir & vbCrLf)
        For Each File In System.IO.Directory.GetFiles(currDir)
            RichTextBox1.SelectionFont = textFont
            RichTextBox1.AppendText("    " & File & vbCrLf)
        Next
        countFiles += System.IO.Directory.GetFiles(currDir).Length
        Me.Text = "scanned " & countFiles & " files in " & countFolders & " folders..."
        For Each Dir In System.IO.Directory.GetDirectories(currDir)
            countFolders += 1
            Application.DoEvents()
            Scanfolder(Dir)
        Next
    End Sub

    Private Sub Folderslist_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FoldersList.DoubleClick
        Dim selDir As String
        selDir = FoldersList.Text
        Dim dirs(), files() As String
        If selDir = ".." Then
            dirs = System.IO.Directory.GetDirectories(parentDir)
            files = System.IO.Directory.GetFiles(parentDir)
            Try
                parentDir = System.IO.Directory.GetParent(parentDir).FullName
            Catch exc As Exception
                parentDir = Nothing
            End Try
        Else
            dirs = System.IO.Directory.GetDirectories(selDir)
            files = System.IO.Directory.GetFiles(selDir)
            Try
                parentDir = System.IO.Directory.GetParent(selDir).FullName
            Catch exc As Exception
                parentDir = Nothing
            End Try
        End If
        Dim dir As String
        FoldersList.Items.Clear()
        If Not parentDir Is Nothing Then
            FoldersList.Items.Add("..")
        End If
        For Each dir In dirs
            FoldersList.Items.Add(dir)
        Next
    End Sub

End Class

⌨️ 快捷键说明

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