Overview of FileSystemWatcher

As C# developers, you may already be familiar with the Windows FileSystemWatcher object and its capabilities. Before we get into writing our FileSystemWatcher, let's talk a little bit about how Windows and you, as a C# developer, handle monitoring changes to the filesystem.

The FileSystemWatcher object in .NET is the object that you will use to monitor a filesystem. Like most other input and output items, it is in the System.IO namespace. The filesystem monitor allows you to monitor directories and file types for changes. You can use it to watch for changes to a specific directory, files and subdirectories of a specific directory, and you can do so on a local computer, remote computer, or networked drive.

You do this by supplying a filter to the watcher so it knows the types of files to monitor for. In many cases, this can be the *.* wildcard.

Alongside the filter, there are several types of changes that you can watch for. They are as follows:

Member name

Description

Attributes

The attributes of the file or folder

CreationTime

The time the file or folder was created

DirectoryName

The name of the directory

FileName

The name of the file

LastAccess

The date the file or folder was last opened

LastWrite

The date the file or folder last had anything written to it

Security

The security settings of the file or folder

Size

The size of the file or folder

 

As well as files, we can also tell filewatcher to monitor subdirectories underneath the folder we are monitoring. This is helpful if your file management approach has, say, a folder for every vendor for instance.

Once a file event happens, the filewatcher object needs to know what to do. This happens by assigning event handlers to several predefined watcher events. The predefined events are:

  • File changed
  • File created
  • File deleted
  • File renamed

At the time of writing, there were some known issues with the filewatcher. They are:

  • Hidden files are not ignored.
  • In some systems, FileSystemWatcher reports changes to files using the short 8.3 filename format. For example, a change to LongFileName.LongExtension could be reported as LongFil~.Lon.
  • This class contains a link demand and an inheritance demand at the class level that applies to all members. A security exception is thrown when either the immediate caller or the derived class does not have full-trust permission.
  • The maximum size you can set for the InternalBufferSize property for monitoring a directory over the network is 64 KB.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset