File system watcher : File system watcher « Stream File « VB.Net Tutorial

VB.Net Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statements
5. Date Time
6. Class Module
7. Development
8. Collections
9. Generics
10. Attributes
11. Event
12. Stream File
13. GUI
14. GUI Applications
15. 2D Graphics
16. I18N Internationlization
17. Reflection
18. Regular Expressions
19. Security
20. Socket Network
21. Thread
22. Windows
23. XML
24. Database ADO.net
25. Design Patterns
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial » Stream File » File system watcher 
12. 11. 2. File system watcher
File system watcher
 

Imports System.IO

Imports System.Windows.Forms

public class FileSystemWatcherDemo
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.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 NothingThen
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtPath As System.Windows.Forms.TextBox
    Friend WithEvents txtFile As System.Windows.Forms.TextBox
    Friend WithEvents txtWatcher As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents btnWatch As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.txtPath = New System.Windows.Forms.TextBox()
        Me.txtFile = New System.Windows.Forms.TextBox()
        Me.txtWatcher = New System.Windows.Forms.TextBox()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.btnWatch = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif"12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label1.Location = New System.Drawing.Point(5120)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(10316)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "File Watcher"
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif"12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label2.Location = New System.Drawing.Point(5152)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(10316)
        Me.Label2.TabIndex = 1
        Me.Label2.Text = "File Type"
        '
        'txtPath
        '
        Me.txtPath.Font = New System.Drawing.Font("Microsoft Sans Serif"10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtPath.Location = New System.Drawing.Point(16416)
        Me.txtPath.Name = "txtPath"
        Me.txtPath.Size = New System.Drawing.Size(65523)
        Me.txtPath.TabIndex = 2
        Me.txtPath.Text = "C:\Temp\test\"
        '
        'txtFile
        '
        Me.txtFile.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtFile.Location = New System.Drawing.Point(164, 48)
        Me.txtFile.Name = "txtFile"
        Me.txtFile.Size = New System.Drawing.Size(133, 23)
        Me.txtFile.TabIndex = 3
        Me.txtFile.Text = "*.txt"
        '
        'txtWatcher
        '
        Me.txtWatcher.Location = New System.Drawing.Point(51, 104)
        Me.txtWatcher.Multiline = True
        Me.txtWatcher.Name = "txtWatcher"
        Me.txtWatcher.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtWatcher.Size = New System.Drawing.Size(778, 224)
        Me.txtWatcher.TabIndex = 5
        Me.txtWatcher.Text = ""
        '
        'Label3
        '
        Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label3.Location = New System.Drawing.Point(51, 80)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(103, 16)
        Me.Label3.TabIndex = 6
        Me.Label3.Text = "Message"
        '
        'btnWatch
        '
        Me.btnWatch.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.btnWatch.Location = New System.Drawing.Point(625, 64)
        Me.btnWatch.Name = "btnWatch"
        Me.btnWatch.Size = New System.Drawing.Size(194, 32)
        Me.btnWatch.TabIndex = 7
        Me.btnWatch.Text = "Start"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(870, 357)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnWatch, Me.Label3, Me.txtWatcher, Me.txtFile, Me.txtPath, Me.Label2, Me.Label1})
        Me.ResumeLayout(False)

    End Sub

    Dim WithEvents FSWatcher As New FileSystemWatcher()

    Private Sub btnWatch_Click _
      (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWatch.Click
        Dim strPathWatch As String
        Dim strFileWatch As String
        strPathWatch = txtPath.Text
        strFileWatch = txtFile.Text
        FSWatcher.Path = strPathWatch
        FSWatcher.Filter = strFileWatch
        FSWatcher.IncludeSubdirectories = True
        FSWatcher.NotifyFilter = _
          NotifyFilters.LastWrite Or NotifyFilters.FileName
        FSWatcher.EnableRaisingEvents = True
        txtWatcher.Text += vbCrLf + "Start:"

    End Sub

    Private Sub FSWatcher_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FSWatcher.Changed
        WatchMessage(e.FullPath, e.ChangeType.ToString)
    End Sub

    Private Sub FSWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FSWatcher.Created
        WatchMessage(e.FullPath, e.ChangeType.ToString)
    End Sub

    Private Sub FSWatcher_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FSWatcher.Deleted
        WatchMessage(e.FullPath, e.ChangeType.ToString)
    End Sub

    Private Sub FSWatcher_Error(ByVal sender As Object, ByVal e As System.IO.ErrorEventArgs) Handles FSWatcher.Error
        WatchMessage("", e.GetException.Message)
    End Sub


    Private Sub FSWatcher_Renamed(ByVal sender As Object, ByVal e As System.IO.RenamedEventArgs) Handles FSWatcher.Renamed
        WatchMessage(e.FullPath, e.ChangeType.ToString)
    End Sub

    Private Sub WatchMessage(ByVal strpFileName As String, ByVal strpMessage As String)
        Dim strMessage As String
        strMessage =  strpFileName + " " + strpMessage
        txtWatcher.Text += vbCrLf + strMessage

    End Sub
End Class

        
12. 11. File system watcher
12. 11. 1. New FileSystemWatcher(Application.StartupPath)
12. 11. 2. File system watcherFile system watcher
ww___w._j___ava___2___s_.__c___o__m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.