System.IO.FileSystemWatcher Class

Listens to the file system change notifications and raises events when a directory, or file in a directory, changes.

See Also: FileSystemWatcher Members

Syntax

[System.ComponentModel.DefaultEvent("Changed")]
[System.IO.IODescription("")]
public class FileSystemWatcher : System.ComponentModel.Component, System.ComponentModel.ISupportInitialize

Remarks

Use System.IO.FileSystemWatcher to watch for changes in a specified directory. You can watch for changes in files and subdirectories of the specified directory. You can create a component to watch files on a local computer, a network drive, or a remote computer.

To watch for changes in all files, set the FileSystemWatcher.Filter property to an empty string ("") or use wildcards ("*.*"). To watch a specific file, set the FileSystemWatcher.Filter property to the file name. For example, to watch for changes in the file MyDoc.txt, set the FileSystemWatcher.Filter property to "MyDoc.txt". You can also watch for changes in a certain type of file. For example, to watch for changes in text files, set the FileSystemWatcher.Filter property to "*.txt".

There are several types of changes you can watch for in a directory or file. For example, you can watch for changes in Attributes, the LastWrite date and time, or the Size of files or directories. This is done by setting the FileSystemWatcher.NotifyFilter property to one of the System.IO.NotifyFilters values. For more information on the type of changes you can watch, see System.IO.NotifyFilters.

You can watch for renaming, deletion, or creation of files or directories. For example, to watch for renaming of text files, set the FileSystemWatcher.Filter property to "*.txt" and call the FileSystemWatcher.WaitForChanged(WatcherChangeTypes) method with a WatcherChangeTypes.Renamed specified for its parameter.

The Windows operating system notifies your component of file changes in a buffer created by the System.IO.FileSystemWatcher. If there are many changes in a short time, the buffer can overflow. This causes the component to lose track of changes in the directory, and it will only provide blanket notification. Increasing the size of the buffer with the FileSystemWatcher.InternalBufferSize property is expensive, as it comes from non-paged memory that cannot be swapped out to disk, so keep the buffer as small yet large enough to not miss any file change events. To avoid a buffer overflow, use the FileSystemWatcher.NotifyFilter and FileSystemWatcher.IncludeSubdirectories properties so you can filter out unwanted change notifications.

For a list of initial property values for an instance of System.IO.FileSystemWatcher, see the FileSystemWatcher.#ctor constructor.

Please note the following when using the System.IO.FileSystemWatcher class.

Note:

Running System.IO.FileSystemWatcher on Windows 98 is not supported.

Copying and moving folders

The operating system and System.IO.FileSystemWatcher object interpret a cut-and-paste action or a move action as a rename action for a folder and its contents. If you cut and paste a folder with files into a folder being watched, the System.IO.FileSystemWatcher object reports only the folder as new, but not its contents because they are essentially only renamed.

To be notified that the contents of folders have been moved or copied into a watched folder, provide FileSystemWatcher.OnChanged(FileSystemEventArgs) and FileSystemWatcher.OnRenamed(RenamedEventArgs) event handler methods as suggested in the following table.

FileSystemWatcher.OnChanged(FileSystemEventArgs)

FileSystemWatcher.Changed, FileSystemWatcher.Created, FileSystemWatcher.Deleted

Report changes in file attributes, created files, and deleted files.

FileSystemWatcher.OnRenamed(RenamedEventArgs)

FileSystemWatcher.Renamed

List the old and new paths of renamed files and folders, expanding recursively if needed.

Events and Buffer Sizes

Note that several factors can affect which file system change events are raised, as described by the following:

Note that a System.IO.FileSystemWatcher may miss an event when the buffer size is exceeded. To avoid missing events, follow these guidelines:

Requirements

Namespace: System.IO
Assembly: System (in System.dll)
Assembly Versions: 1.0.3300.0, 1.0.5000.0, 2.0.0.0, 4.0.0.0