NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

FileSystemWatcher Class

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

Object
   Component
      FileSystemWatcher

[Visual Basic]
Public Class FileSystemWatcher
   Inherits Component
   Implements ISupportInitialize
[C#]
public class FileSystemWatcher : Component, ISupportInitialize
[C++]
public __gc class FileSystemWatcher : public Component,
   ISupportInitialize
[JScript]
public class FileSystemWatcher extends Component,
   ISupportInitialize

Remarks

Use FileSystemWatcher to watch for changes in a specified directory. You can watch for changes in both files and subdirectories of the specified directory. The component can be used to watch files on your personal computer, a network drive, or a remote machine.

Note   You cannot watch a remote computer that does not have Windows NT or Windows 2000 installed. You cannot watch a remote Windows NT 4.0 computer from a Windows NT 4.0 computer.

To watch for changes in all files, set the Filter property to "*.*". To watch a specific file, set the Filter property to the file name. For example, if you want to watch for any changes in the file MyDoc.txt, set the Filter property to "MyDoc.txt". You can also watch for changes in a certain type of file. For example, to watch for changes in any text files, set the Filter property to "*.txt".

Note   Hidden files are not ignored.

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 ChangedFilter property to one of the ChangedFilters values. For more information on the type of changes you can watch for, see ChangedFilters. For more information on using the ChangedFilter property, see ChangedFilter.

You can watch for any renaming, deletion, or creation of files or directories. For example, to watch for any renaming of text files, set the Filter property to "*.txt" and call one of the WaitForChanged methods with the WatcherChangeTypes value Renamed given.

The system notifies you of file changes in a buffer that the component creates and passes to the Application Programming Interfaces. If there are a lot of changes in a short amount of time, the buffer can overflow. This causes the component to lose track of changes in the directory, and it will only be able to provide a blanket notification. Increasing the size of the buffer is expensive, as it comes from non-paged memory that cannot be swapped out to disk, so you want to keep the buffer as small as possible. To avoid a buffer overflow, use the ChangedFilter, IncludeSubdirectories, and Target properties so you can filter out your unwanted change notifications. For more information on the buffer size, see InternalBufferSize.

Note   Setting the Filter does not decrease what goes into the buffer.

When an instance of FileSystemWatcher is created, some of the read/write properties are set to initial values. For a list of these values, see the FileSystemWatcher constructor.

Requirements

Namespace: System.IO

Assembly: system.io.dll

See Also

FileSystemWatcher Members | System.IO Namespace | ChangedFilter | ChangedFilters | FileSystemEventArgs | FileSystemEventHandler | Filter | IncludeSubdirectories | InternalBufferOverflowException | RenamedEventArgs | RenamedEventHandler | Target | WaitForChangedResult | WatcherChangeTypes | WatcherTarget