FileSystemWatcher in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes the FileSystemWatcher control


FileSystemWatcher

The FileSystemWatcher class lets an application watch for changes to a file or directory and raises events (if EnableRaisingEvents is True) to let your program know if something changes. For example, it will notify your program if a file is created in a certain directory.

Its Filter property determines the files for which the watcher reports events, but please be aware that you cannot watch for multiple file types. Rather you have to use multiple FileSystemWatcher instances. However the default Filter value is *.*, which means it will watch all the files.

Its IncludeSubdirectories property determines whether the object watches subdirectories within the main path (Path property which determines the path to watch).

properties

    ' where our executable resides
    FileSystemWatcher1.Path = Application.StartupPath


The following table summarizes the FileSystemWatcher's events:

NAME DESCRIPTION
Changed A file or directory has changed.
Created A file or directory was created.
Deleted A file or directory was deleted.
Renamed A file or directory was renamed.
Error The watcher's internal buffer overflowed.

    Private Sub FileSystemWatcher_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' set the path to be watched
        FileSystemWatcher1.Path = Application.StartupPath

        ' add event handlers
        AddHandler FileSystemWatcher1.Changed, AddressOf OnChanged
        AddHandler FileSystemWatcher1.Created, AddressOf OnCreated
        AddHandler FileSystemWatcher1.Deleted, AddressOf OnDeleted
        AddHandler FileSystemWatcher1.Renamed, AddressOf OnRenamed
        AddHandler FileSystemWatcher1.Error, AddressOf OnError
    End Sub

    Private Sub OnChanged(sender As Object, e As IO.FileSystemEventArgs)
        ' print some info about the file that is changed
        Console.WriteLine("File: " & e.Name & " " & e.ChangeType)
    End Sub
    Private Sub OnCreated(sender As Object, e As IO.FileSystemEventArgs)
        ' print some info about the file that is created
        Console.WriteLine("File: {0} is created" & e.Name)
    End Sub
    Private Sub OnDeleted(sender As Object, e As IO.FileSystemEventArgs)
        ' print some info about the file that is deleted
        Console.WriteLine("File: {0} has been deleted" & e.Name)
    End Sub
    Private Sub OnRenamed(sender As Object, e As IO.RenamedEventArgs)
        ' print some info about the file that is renamed
        Console.WriteLine("File: {0} renamed to {1}" & e.OldFullPath, e.FullPath)
    End Sub
    Private Sub OnError(sender As Object, e As IO.ErrorEventArgs)
        ' print some info about the error
        Console.WriteLine("An error occurred: " & e.GetException.Message)
    End Sub

Previous Lesson | Next Lesson


Share This

Subscribe

More Tutorials

Popular

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.