精选文章 FileSystemWatcher类的使用----- vb2010实例 磁盘与文件夹监视器

FileSystemWatcher类的使用----- vb2010实例 磁盘与文件夹监视器

作者:jinwei29 时间: 2019-11-05 12:44:16
jinwei29 2019-11-05 12:44:16

        此实例包含FileSystemWatcher类,clListBox背景透明,类的重写,设置控件可以用透明背景Transparent,从不是创建控件“richTextBox1”的线程访问它,利用线程的解决方案,还有无边窗体移动方法,任务栏右侧通知显示图标方法等,值得新手研究。

FileSystemWatcher类的使用----- vb2010实例 磁盘与文件夹监视器1

FileSystemWatcher类的使用----- vb2010实例 磁盘与文件夹监视器2

 



Public Class Form1
    Private IsFormBeingDragged As Boolean = False '拖运窗体
    Private MouseDownX As Integer '按下时的x坐标
    Private MouseDownY As Integer '按下时的y坐标
    Dim fs As New IO.FileSystemWatcher


    ''' 
    ''' 获取要监视的路径
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub Bt_Open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Open.Click
        Dim fb As New FolderBrowserDialog '建一个打开文件夹的实例化对象
        If fb.ShowDialog = Windows.Forms.DialogResult.OK Then '如果打开了文件夹并返回为OK为真
            fs.Path = fb.SelectedPath '获取返回的路径
            Bt_Strat.Enabled = True '开启按钮可用
        End If
    End Sub

    ''' 
    ''' 开启文件夹监视
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub Bt_Strat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Strat.Click
        fs.EnableRaisingEvents = True '开启文件夹监视
        Bt_Stop.Enabled = True '停止按钮可用
        Bt_Strat.Enabled = False '启动按钮不可用
        Label1.Text = "服务启动"



        AddHandler fs.Created, AddressOf OnCreate '定义文件创建的事件委托
        AddHandler fs.Deleted, AddressOf Ondeleted '定义删除文件的事件委托
        AddHandler fs.Renamed, AddressOf OnRename '定义文件被重命名的事件委托
    End Sub

    ''' 
    ''' 关闭文件监控
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub Bt_Stop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Stop.Click
        fs.EnableRaisingEvents = False
        Bt_Open.Enabled = True
        Bt_Strat.Enabled = False
        Label1.Text = "服务停止"
    End Sub

    ''' 
    ''' 文件创建时触发
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub OnCreate(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
        SetClListBox1("时间:" & My.Computer.Clock.LocalTime & "  " & e.Name & " 动作:创建   路径: " & e.FullPath)
        SetText(vbNewLine & ("时间: " & My.Computer.Clock.LocalTime & "  " & e.Name & " 动作:创建  路径: " & e.FullPath))
    End Sub

    ''' 
    ''' 文件被删除时引发
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub Ondeleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
        SetClListBox1("时间: " & My.Computer.Clock.LocalTime & " " & e.Name & " 动作:删除   路径:" & e.FullPath)
        SetText(vbNewLine & ("时间: " & My.Computer.Clock.LocalTime & " " & e.Name & " 动作:删除   路径:" & e.FullPath))
    End Sub

    ''' 
    ''' 文件被重命名时引发
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub OnRename(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs)
        SetClListBox1("时间: " & My.Computer.Clock.LocalTime & "  " & e.OldName & "  动作:更改名称:  " & e.Name)
        SetText(vbNewLine & ("时间: " & My.Computer.Clock.LocalTime & "  " & e.OldName & "  动作:更改名称: " & e.Name))
    End Sub

#Region "线程委托"
    Private Delegate Sub SetTextCallback(ByVal text As String) '定义的委托声明
    '在给textBox1.text赋值的地方调用以下方法即可
    Private Sub SetClListBox1(ByVal text As String)
        ' InvokeRequired需要比较调用线程ID和创建线程ID
        ' 如果它们不相同则返回true
        If Me.ClListBox1.InvokeRequired Then
            Dim d As New SetTextCallback(AddressOf SetClListBox1)
            Me.Invoke(d, New Object() {text})
        Else
            ClListBox1.Items.Add(text)
        End If
    End Sub

    Private Delegate Sub SetRichTextBox1Callback(ByVal text As String) '定义的委托声明
    ''' 
    ''' 定义设置RichTextBox1文本的委托,因为监视不在一个线程上
    ''' 
    ''' 
    ''' 
    Private Sub SetText(ByVal text As String)
        ' InvokeRequired需要比较调用线程ID和创建线程ID
        ' 如果它们不相同则返回true
        If Me.RichTextBox1.InvokeRequired Then
            Dim d As New SetRichTextBox1Callback(AddressOf SetText)
            Me.Invoke(d, New Object() {text})
        Else
            RichTextBox1.AppendText(text)
        End If
    End Sub

    'Private Delegate Sub SetRichTextBox1Callback(ByVal text As String)
    'Private Sub SetText(ByVal text As String)
    '    If Me.richTextBox1.InvokeRequired Then
    '        Dim stcb As New SetRichTextBox1Callback(AddressOf SetText)
    '        Me.Invoke(stcb, New Object() {text})
    '    Else
    '        Me.richTextBox1.Text = text
    '    End If
    'End Sub


#End Region


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false
    End Sub


    ''' 
    ''' 定义的时钟,间隔为10,监视Tb_Rt中文本的值
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label7.Text = My.Computer.Clock.LocalTime '获取计算机的本地时间
        If RichTextBox1.TextLength = 0 Then '获取控件中文本的长度
            Button4.Enabled = False '文本框中有文本才能倒出
        Else
            Button4.Enabled = True
        End If
    End Sub

#Region "鼠标拖动窗体"

    ''' 
    ''' 窗体鼠标按下事件,写鼠标拖动窗体,按下鼠标左键,
    ''' 捕获鼠标事件,得到按下时的x、y坐标
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub Form1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then '如果按下的键为鼠标左键
            IsFormBeingDragged = True '窗体拖动为真
            MouseDownX = e.X '获取鼠标在产生鼠标事件时的x坐标
            MouseDownY = e.Y
        End If

    End Sub

    ''' 
    ''' 按下鼠标移动,计算起点和终点坐标,拖动窗体
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        If IsFormBeingDragged Then '如果拖动窗体为真
            Dim TEMP As Point = New Point '定义平面上的点
            TEMP.X = Me.Location.X + (e.X - MouseDownX)
            TEMP.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = TEMP '获取或设置窗体左上角的点
        End If
    End Sub


    ''' 
    ''' 鼠标抬起事件,结束窗体移动
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub Form1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        If e.Button = MouseButtons.Left Then '如果抬起的是鼠标左键
            IsFormBeingDragged = False
        End If
    End Sub

#End Region

    ''' 
    ''' 单击最小化按钮时,最小化窗体,在任务栏右边通知上显示图标
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub Bt_min_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_min.Click
        Me.WindowState = FormWindowState.Minimized  '设置窗体为最小化
        If Me.WindowState = FormWindowState.Minimized Then
            Me.Hide() '隐藏窗体
            NotifyIcon1.Visible = True '在通知栏上显示图标
        End If
    End Sub

    ''' 
    ''' 处理--双击任务栏图标时显示窗体
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub ToolStrip1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ToolStrip1.MouseDoubleClick
        Me.Show() '显示窗体
        Me.WindowState = FormWindowState.Normal '以标准模式显示窗体
        NotifyIcon1.Visible = False '不在任务栏右边的通知栏上显示图标
    End Sub


    ''' 
    ''' 关闭窗体
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub Bt_Close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt_Close.Click
        Me.Close() '关闭窗体
    End Sub

    ''' 
    ''' 倒出监控到的日志
    ''' 
    ''' 
    ''' 
    ''' 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        RichTextBox1.SaveFile("C:\TT.RTF")
    End Sub

   
End Class

源码下载:https://download.csdn.net/download/jinwei29/11958586

勿删,copyright占位
分享文章到微博
分享文章到朋友圈

上一篇:(二) 数据结构 - 插入排序

下一篇:OpenInfra上海峰会:2019出现这些新变化

CSDN

CSDN

中国开发者社区CSDN (Chinese Software Developer Network) 创立于1999年,致力为中国开发者提供知识传播、在线学习、职业发展等全生命周期服务。