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

📄 form1.vb

📁 Mastering VBNet Include Source Code
💻 VB
字号:
Option Strict On
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.
    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 Button1 As System.Windows.Forms.Button
    Friend WithEvents FileSystemWatcher1 As System.IO.FileSystemWatcher
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox

    '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.Button1 = New System.Windows.Forms.Button()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.FileSystemWatcher1 = New System.IO.FileSystemWatcher()
        CType(Me.FileSystemWatcher1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Button1.Location = New System.Drawing.Point(80, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(256, 32)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Start Monitoring"
        '
        'ListBox1
        '
        Me.ListBox1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.ListBox1.ItemHeight = 14
        Me.ListBox1.Location = New System.Drawing.Point(8, 56)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(408, 186)
        Me.ListBox1.TabIndex = 1
        '
        'FileSystemWatcher1
        '
        Me.FileSystemWatcher1.EnableRaisingEvents = True
        Me.FileSystemWatcher1.NotifyFilter = ((System.IO.NotifyFilters.FileName Or System.IO.NotifyFilters.DirectoryName) _
                    Or System.IO.NotifyFilters.LastWrite)
        Me.FileSystemWatcher1.Path = "C:\"
        Me.FileSystemWatcher1.SynchronizingObject = Me
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(424, 253)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListBox1, Me.Button1})
        Me.Name = "Form1"
        Me.Text = "FileSystemWatcher Demo"
        CType(Me.FileSystemWatcher1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "Start Monitoring" Then
            FileSystemWatcher1.EnableRaisingEvents = True
            Button1.Text = "Stop Monitoring"
        Else
            FileSystemWatcher1.EnableRaisingEvents = False
            Button1.Text = "Start Monitoring"
        End If
    End Sub

    Private Sub FileSystemWatcher1_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed, FileSystemWatcher1.Created, FileSystemWatcher1.Deleted
        Select Case e.ChangeType
            Case IO.WatcherChangeTypes.Changed
                ListBox1.Items.Add("CHANGED" & vbTab & e.FullPath)
            Case IO.WatcherChangeTypes.Created
                ListBox1.Items.Add("CREATED" & vbTab & e.FullPath)
            Case IO.WatcherChangeTypes.Deleted
                ListBox1.Items.Add("DELETED " & vbTab & e.FullPath)
        End Select
    End Sub

    Private Sub FileSystemWatcher1_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed
        ListBox1.Items.Add("RENAMED" & vbTab & e.OldFullPath & " TO " & e.FullPath)
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FileSystemWatcher1.Path = "c:\"
        FileSystemWatcher1.IncludeSubdirectories = False
        FileSystemWatcher1.Filter = "*.txt"
        FileSystemWatcher1.NotifyFilter = IO.NotifyFilters.CreationTime Or IO.NotifyFilters.LastWrite Or IO.NotifyFilters.LastAccess Or IO.NotifyFilters.FileName
        FileSystemWatcher1.EnableRaisingEvents = False
    End Sub

    Private Sub FileSystemWatcher1_Error(ByVal sender As Object, ByVal e As System.IO.ErrorEventArgs) Handles FileSystemWatcher1.Error

        If FileSystemWatcher1.EnableRaisingEvents Then
            FileSystemWatcher1.EnableRaisingEvents = False
            FileSystemWatcher1.InternalBufferSize = 2 * _
                                 FileSystemWatcher1.InternalBufferSize
            FileSystemWatcher1.EnableRaisingEvents = True
        End If
    End Sub
End Class

⌨️ 快捷键说明

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