FileSystemWatcher Class

Definition

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

public class FileSystemWatcher : IDisposable
Inheritance
FileSystemWatcher

Inherited Members

System.Object

Constructors

FileSystemWatcher()

FileSystemWatcher(String)

Initializes a new instance of the FileSystemWatcher class, given the specified directory to monitor.

public FileSystemWatcher(string path)
Parameters
path
String

The directory to monitor, in standard or Universal Naming Convention (UNC) notation.

Exceptions

The path parameter is null.

The path parameter is an empty string ("").

-or-

The path specified through the path parameter does not exist.

path is too long.

See Also

FileSystemWatcher(String, String)

Initializes a new instance of the FileSystemWatcher class, given the specified directory and type of files to monitor.

public FileSystemWatcher(string path, string filter)
Parameters
path
String

The directory to monitor, in standard or Universal Naming Convention (UNC) notation.

filter
String

The type of files to watch. For example, "*.txt" watches for changes to all text files.

Exceptions

The path parameter is null.

-or-

The filter parameter is null.

The path parameter is an empty string ("").

-or-

The path specified through the path parameter does not exist.

path is too long.

See Also

Properties

EnableRaisingEvents

Gets or sets a value indicating whether the component is enabled.

public bool EnableRaisingEvents { get; set; }
Value
Boolean

true if the component is enabled; otherwise, false. The default is false. If you are using the component on a designer in Visual Studio 2005, the default is true.

Exceptions

The FileSystemWatcher object has been disposed.

The current operating system is not Microsoft Windows NT or later.

The directory specified in Path could not be found.

Path has not been set or is invalid.

Filter

Gets or sets the filter string used to determine what files are monitored in a directory.

public string Filter { get; set; }
Value
String

The filter string. The default is "." (Watches all files.)

See Also

IncludeSubdirectories

Gets or sets a value indicating whether subdirectories within the specified path should be monitored.

public bool IncludeSubdirectories { get; set; }
Value
Boolean

true if you want to monitor subdirectories; otherwise, false. The default is false.

See Also

InternalBufferSize

Gets or sets the size (in bytes) of the internal buffer.

public int InternalBufferSize { get; set; }
Value
Int32

The internal buffer size in bytes. The default is 8192 (8 KB).

See Also

NotifyFilter

Gets or sets the type of changes to watch for.

public NotifyFilters NotifyFilter { get; set; }
Value
NotifyFilters

One of the NotifyFilters values. The default is the bitwise OR combination of LastWrite, FileName, and DirectoryName.

Exceptions

The value is not a valid bitwise OR combination of the NotifyFilters values.

The value that is being set is not valid.

See Also

Path

Gets or sets the path of the directory to watch.

public string Path { get; set; }
Value
String

The path to monitor. The default is an empty string ("").

Exceptions

The specified path does not exist or could not be found.

-or-

The specified path contains wildcard characters.

-or-

The specified path contains invalid path characters.

See Also

Methods

Dispose()

public void Dispose()

Dispose(Boolean)

Releases the unmanaged resources used by the FileSystemWatcher and optionally releases the managed resources.

protected virtual void Dispose(bool disposing)
Parameters
disposing
Boolean

true to release both managed and unmanaged resources; false to release only unmanaged resources.

OnChanged(FileSystemEventArgs)

Raises the Changed event.

protected void OnChanged(FileSystemEventArgs e)
Parameters
e
FileSystemEventArgs

A FileSystemEventArgs that contains the event data.

See Also

OnCreated(FileSystemEventArgs)

Raises the Created event.

protected void OnCreated(FileSystemEventArgs e)
Parameters
e
FileSystemEventArgs

A FileSystemEventArgs that contains the event data.

See Also

OnDeleted(FileSystemEventArgs)

Raises the Deleted event.

protected void OnDeleted(FileSystemEventArgs e)
Parameters
e
FileSystemEventArgs

A FileSystemEventArgs that contains the event data.

See Also

OnError(ErrorEventArgs)

Raises the Error event.

protected void OnError(ErrorEventArgs e)
Parameters
e
ErrorEventArgs

An ErrorEventArgs that contains the event data.

See Also

OnRenamed(RenamedEventArgs)

Raises the Renamed event.

protected void OnRenamed(RenamedEventArgs e)
Parameters
e
RenamedEventArgs

A RenamedEventArgs that contains the event data.

See Also

WaitForChanged(WatcherChangeTypes)

A synchronous method that returns a structure that contains specific information on the change that occurred, given the type of change you want to monitor.

public WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType)
Parameters
changeType
WatcherChangeTypes

The WatcherChangeTypes to watch for.

Returns

A WaitForChangedResult that contains specific information on the change that occurred.

See Also

WaitForChanged(WatcherChangeTypes, Int32)

A synchronous method that returns a structure that contains specific information on the change that occurred, given the type of change you want to monitor and the time (in milliseconds) to wait before timing out.

public WaitForChangedResult WaitForChanged(WatcherChangeTypes changeType, int timeout)
Parameters
changeType
WatcherChangeTypes

The WatcherChangeTypes to watch for.

timeout
Int32

The time (in milliseconds) to wait before timing out.

Returns

A WaitForChangedResult that contains specific information on the change that occurred.

See Also

Events

Changed

Occurs when a file or directory in the specified Path is changed.

public event FileSystemEventHandler Changed
See Also

Created

Occurs when a file or directory in the specified Path is created.

public event FileSystemEventHandler Created
See Also

Deleted

Occurs when a file or directory in the specified Path is deleted.

public event FileSystemEventHandler Deleted
See Also

Error

Occurs when the instance of FileSystemWatcher is unable to continue monitoring changes or when the internal buffer overflows.

public event ErrorEventHandler Error
See Also

Renamed

Occurs when a file or directory in the specified Path is renamed.

public event RenamedEventHandler Renamed
See Also