form1.vb

来自「vb 应用实例 简单应用 dddddddddddddddddddddddddd」· VB 代码 · 共 152 行

VB
152
字号
Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form "

    Public Sub New()
        MyBase.New()

        InitializeComponent()


    End Sub

   
    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

    Private components As System.ComponentModel.IContainer

   
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TreeView1 = New System.Windows.Forms.TreeView()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        '
        'TreeView1
        '
        Me.TreeView1.ImageIndex = -1
        Me.TreeView1.Location = New System.Drawing.Point(20, 16)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.SelectedImageIndex = -1
        Me.TreeView1.Size = New System.Drawing.Size(356, 312)
        Me.TreeView1.TabIndex = 0
        '
        'TextBox1
        '
        Me.TextBox1.BackColor = System.Drawing.SystemColors.HighlightText
        Me.TextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.TextBox1.Location = New System.Drawing.Point(400, 16)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ReadOnly = True
        Me.TextBox1.Size = New System.Drawing.Size(389, 320)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(808, 349)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1, Me.TreeView1})
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "文件管理"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadTreeView()
    End Sub

    Private Sub LoadTreeView()
        Dim strDriveID As String

        TreeView1.Nodes.Clear()
        For Each strDriveID In Directory.GetLogicalDrives()
            With TreeView1.Nodes.Add(strDriveID)
                .Nodes.Add("?")   
            End With
        Next
    End Sub

    Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
        Try
            e.Node.Nodes.Clear()
            AddFoldersFiles(e.Node)

        Catch exp As IOException
            'MsgBox(exp.ToString, Me.Text)
        Catch exp As Exception
            MsgBox(exp.ToString, Me.Text)
        End Try
    End Sub

    Private Sub AddFoldersFiles(ByVal node1 As TreeNode)
        Dim strPath As String = node1.FullPath
        Dim strDir, strFile As String

        'AddFolders
        With node1
            For Each strDir In Directory.GetDirectories(strPath)
                With node1.Nodes.Add(Path.GetFileName(strDir))
                    .Tag = 1    'Directory
                    .Nodes.Add("?")    
                End With
            Next
        End With

        'AddFiles
        With node1
            For Each strFile In Directory.GetFiles(strPath)
                With node1.Nodes.Add(Path.GetFileName(strFile))
                    .Tag = 2    'File
                End With
            Next
        End With
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        If e.Node.Tag <> 1 And e.Node.Tag <> 2 Then Return
        Try
            TextBox1.Text = ""

            If e.Node.Tag = 1 Then  'Directory
                Dim di As New DirectoryInfo(e.Node.FullPath)
                TextBox1.Text += "Root = " & di.Root.Name & vbCrLf
                TextBox1.Text += "Parent = " & di.Parent.Name & vbCrLf
                TextBox1.Text += "Attributes = " & di.Attributes.ToString & vbCrLf
                TextBox1.Text += "CreationTime = " & di.CreationTime.ToString & vbCrLf
                TextBox1.Text += "LastAccessTime = " & di.LastAccessTime.ToString & vbCrLf
                TextBox1.Text += "FullName = " & di.FullName & vbCrLf

            Else 'File
                Dim fi As New FileInfo(e.Node.FullPath)
                TextBox1.Text += "Length = " & fi.Length & vbCrLf
                TextBox1.Text += "Attributes = " & fi.Attributes.ToString & vbCrLf
                TextBox1.Text += "CreationTime = " & fi.CreationTime.ToString & vbCrLf
                TextBox1.Text += "LastAccessTime = " & fi.LastAccessTime.ToString & vbCrLf
                TextBox1.Text += "Extension = " & fi.Extension & vbCrLf
                TextBox1.Text += "FullName = " & fi.FullName & vbCrLf
            End If

        Catch exp As Exception
            MsgBox(exp.Message, Me.Text)
        End Try
    End Sub

End Class

⌨️ 快捷键说明

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