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

📄 form1.vb

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

    Private WithEvents ToolTip1 As System.Windows.Forms.ToolTip
    Private WithEvents DrivesList As System.Windows.Forms.ComboBox
    Private WithEvents FoldersList As System.Windows.Forms.ListBox
    Private WithEvents FilesList As System.Windows.Forms.ListBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Label1 As System.Windows.Forms.Label
    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.FoldersList = New System.Windows.Forms.ListBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.DrivesList = New System.Windows.Forms.ComboBox()
        Me.FilesList = New System.Windows.Forms.ListBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'ToolTip1
        '
        Me.ToolTip1.ShowAlways = True
        '
        'FoldersList
        '
        Me.FoldersList.Font = New System.Drawing.Font("Trebuchet MS", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FoldersList.ItemHeight = 18
        Me.FoldersList.Location = New System.Drawing.Point(192, 8)
        Me.FoldersList.Name = "FoldersList"
        Me.FoldersList.Size = New System.Drawing.Size(392, 112)
        Me.FoldersList.TabIndex = 1
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Trebuchet MS", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(8, 128)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(568, 16)
        Me.Label1.TabIndex = 4
        '
        'DrivesList
        '
        Me.DrivesList.DropDownWidth = 160
        Me.DrivesList.Font = New System.Drawing.Font("Trebuchet MS", 9.75!, 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(176, 26)
        Me.DrivesList.TabIndex = 0
        Me.DrivesList.Text = "ComboBox1"
        '
        'FilesList
        '
        Me.FilesList.Font = New System.Drawing.Font("Trebuchet MS", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FilesList.ItemHeight = 18
        Me.FilesList.Location = New System.Drawing.Point(8, 144)
        Me.FilesList.Name = "FilesList"
        Me.FilesList.Size = New System.Drawing.Size(576, 202)
        Me.FilesList.TabIndex = 2
        '
        'Button1
        '
        Me.Button1.Font = New System.Drawing.Font("Trebuchet MS", 12!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button1.Location = New System.Drawing.Point(8, 88)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(176, 32)
        Me.Button1.TabIndex = 3
        Me.Button1.Text = "Scan Selected Folder"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(592, 349)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.Button1, Me.FilesList, Me.FoldersList, Me.DrivesList})
        Me.Name = "Form1"
        Me.Text = "FileScan"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Dim parentDir As String
    Dim countFiles, countFolders As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim drives() As String
        drives = 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 DrivesList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DrivesList.SelectedIndexChanged
        Dim directories() As String
        Try
            directories = Directory.GetDirectories(DrivesList.Text)
        Catch drvException As Exception
            MsgBox(drvException.Message)
            Exit Sub
        End Try
        Dim dir As String
        FoldersList.Items.Clear()
        FilesList.Items.Clear()
        For Each dir In directories
            FoldersList.Items.Add(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 = Directory.GetDirectories(parentDir)
            files = Directory.GetFiles(parentDir)
            Try
                parentDir = Directory.GetParent(parentDir).FullName
            Catch exc As Exception
                parentDir = Nothing
            End Try
        Else
                dirs = Directory.GetDirectories(selDir)
                files = Directory.GetFiles(selDir)
            Try
                parentDir = 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

    Dim interrupt As Boolean
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        countFiles = 0
        countFolders = 0
        FilesList.Items.Clear()
        If Button1.Text = "Scan Selected Folder" Then
            Button1.Text = "Stop Scanning"
            interrupt = False
        Else
            Button1.Text = "Scan Selected Folder"
            interrupt = True
            Me.Text = "INTERRUPTED"
        End If
        Try
            If FoldersList.Text = "" Then
                Scanfolder(DrivesList.Text)
            Else
                Scanfolder(FoldersList.Text)
            End If
        Catch scanException As Exception
            MsgBox(scanException.Message)
        End Try
        Button1.Text = "Scan Selected Folder"
    End Sub

    Sub Scanfolder(ByVal currDir As String)
        If interrupt Then Exit Sub
        Dim Dir As String
        Dim File As String
        Dim FI As FileInfo
        Label1.Text = "scanning " & currDir
        For Each File In Directory.GetFiles(currDir)
            FI = New FileInfo(File)
            FilesList.Items.Add(FI.Name & vbTab & FI.Length & vbTab & FI.CreationTime)
        Next
        countFiles += Directory.GetFiles(currDir).Length
        Me.Text = "Scanning " & FoldersList.Text & _
                    " -- Processed " & countFiles & " files in " & countFolders & " folders..."
        For Each Dir In Directory.GetDirectories(currDir)
            countFolders += 1
            Application.DoEvents()
            Scanfolder(Dir)
        Next
    End Sub
End Class

⌨️ 快捷键说明

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