Listens to the file system change notifications and raises events when a directory, or file in a directory, changes.
To browse the .NET Framework source code for this type, see the Reference Source.
Assembly: System (in System.dll)
[<IODescriptionAttribute("FileSystemWatcherDesc")>] [<PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")>] [<PermissionSetAttribute(SecurityAction.InheritanceDemand, Name = "FullTrust")>] type FileSystemWatcher = class inherit Component interface ISupportInitialize end
Thetype exposes the following members.
|FileSystemWatcher()||Initializes a new instance of the class.|
|FileSystemWatcher(String)||Initializes a new instance of the class, given the specified directory to monitor.|
|FileSystemWatcher(String, String)||Initializes a new instance of the class, given the specified directory and type of files to monitor.|
|CanRaiseEvents||Gets a value indicating whether the component can raise an event. (Inherited from Component.)|
|Container||Gets the IContainer that contains the Component. (Inherited from Component.)|
|DesignMode||Gets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)|
|EnableRaisingEvents||Gets or sets a value indicating whether the component is enabled.|
|Events||Gets the list of event handlers that are attached to this Component. (Inherited from Component.)|
|Filter||Gets or sets the filter string used to determine what files are monitored in a directory.|
|IncludeSubdirectories||Gets or sets a value indicating whether subdirectories within the specified path should be monitored.|
|InternalBufferSize||Gets or sets the size (in bytes) of the internal buffer.|
|NotifyFilter||Gets or sets the type of changes to watch for.|
|Path||Gets or sets the path of the directory to watch.|
|Site||Gets or sets an ISite for the . (Overrides Component.Site.)|
|SynchronizingObject||Gets or sets the object used to marshal the event handler calls issued as a result of a directory change.|
|BeginInit||Begins the initialization of a used on a form or used by another component. The initialization occurs at run time.|
|CreateObjRef||Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)|
|Dispose()||Releases all resources used by the Component. (Inherited from Component.)|
|Dispose(Boolean)||Releases the unmanaged resources used by the and optionally releases the managed resources. (Overrides Component.Dispose(Boolean).)|
|EndInit||Ends the initialization of a used on a form or used by another component. The initialization occurs at run time.|
|Equals(Object)||Determines whether the specified object is equal to the current object. (Inherited from Object.)|
|Finalize||Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)|
|GetHashCode||Serves as the default hash function. (Inherited from Object.)|
|GetLifetimeService||Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)|
|GetService||Returns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|InitializeLifetimeService||Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)|
|MemberwiseClone()||Creates a shallow copy of the current Object. (Inherited from Object.)|
|MemberwiseClone(Boolean)||Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)|
|OnChanged||Raises the Changed event.|
|OnCreated||Raises the Created event.|
|OnDeleted||Raises the Deleted event.|
|OnError||Raises the Error event.|
|OnRenamed||Raises the Renamed event.|
|ToString||Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.)|
|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.|
|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.|
|Changed||Occurs when a file or directory in the specified Path is changed.|
|Created||Occurs when a file or directory in the specified Path is created.|
|Deleted||Occurs when a file or directory in the specified Path is deleted.|
|Disposed||Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)|
|Error||Occurs when the instance of is unable to continue monitoring changes or when the internal buffer overflows.|
|Renamed||Occurs when a file or directory in the specified Path is renamed.|
To view the .NET Framework source code for this type, see the Reference Source. You can browse through the source code online, download the reference for offline viewing, and step through the sources (including patches and updates) during debugging; see instructions.
Use 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 Filter property to an empty string ("") or use wildcards ("*.*"). To watch a specific file, set the Filter property to the file name. For example, to watch for 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 text files, set the 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 NotifyFilter property to one of the NotifyFilters values. For more information on the type of changes you can watch, see NotifyFilters.
You can watch for renaming, deletion, or creation of files or directories. For example, to watch for renaming of text files, set the Filter property to "*.txt" and call the WaitForChanged method with a Renamed specified for its parameter.
The Windows operating system notifies your component of file changes in a buffer created by the . 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 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 NotifyFilter and IncludeSubdirectories properties so you can filter out unwanted change notifications.
For a list of initial property values for an instance of , see the FileSystemWatcher constructor.
Please note the following when using the class.
Hidden files are not ignored.
In some systems, reports changes to files using the short 8.3 file name 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 SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission. For details about security demands, see Link Demands.
The maximum size you can set for the InternalBufferSize property for monitoring a directory over the network is 64 KB.
Running on Windows 98 is not supported.
Copying and moving folders
The operating system and 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 object reports only the folder as new, but not its contents because they are essentially only renamed.
Events and Buffer Sizes
Note that several factors can affect which file system change events are raised, as described by the following:
Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by .
The can watch disks as long as they are not switched or removed. The does not raise events for CDs and DVDs, because time stamps and properties cannot change. Remote computers must have one of the required platforms installed for the component to function properly.
If multiple objects are watching the same UNC path in Windows XP prior to Service Pack 1, or Windows 2000 SP2 or earlier, then only one of the objects will raise an event. On machines running Windows XP SP1 and newer, Windows 2000 SP3 or newer or Windows Server 2003, all objects will raise the appropriate events.
Note that a may miss an event when the buffer size is exceeded. To avoid missing events, follow these guidelines:
Increase the buffer size by setting the InternalBufferSize property.
Avoid watching files with long file names, because a long file name contributes to filling up the buffer. Consider renaming these files using shorter names.
Keep your event handling code as short as possible.
The following example creates a to watch the directory specified at run time. The component is set to watch for changes in LastWrite and LastAccess time, the creation, deletion, or renaming of text files in the directory. If a file is changed, created, or deleted, the path to the file prints to the console. When a file is renamed, the old and new paths print to the console.
For a sample that uses the class, see Using a FileSystemWatcher Component in a Windows Form