C # filesystemwatcher usage when monitoring folders and files

Source: Internet
Author: User

 

Overview

I recently learned how to use filesystemwatcher. It is mainly used to monitor a folder. If there are any changes to the files in the folder, I will record my understanding and usage of filesystemwatcher.

Filesystemwatcher usage

Before applying the filesystemwatcher object, you must understand some basic attributes and events of this object. Undoubtedly, the most important attribute of this object is the "enableraisingevents" attribute.

This attribute determines whether the object submits an event when it receives the change notification. If the enableraisingevents attribute is set to false, the object will not submit the change event. If it is set to true, it submits a change event. The following are some other important attributes/events that you will use when applying the filesystemwatcher object:

Attribute:

Path -- this attribute tells filesystemwatcher which path it needs to monitor. For example, if we set this attribute to "C: temp", the object will monitor all the changes in that directory.
Includesubdirectories -- this attribute indicates whether the filesystemwatcher object should monitor changes in subdirectories.
Filter -- this attribute allows you to filter out changes to some types of files. For example, if we only want to submit a notification when the TXT file is modified, created, or deleted, we can set this attribute to "* TXT ". This attribute is very convenient when processing high-traffic or large directories.
Event

Changed -- this event is submitted when a file in the monitored directory is modified. It is worth noting that this event may be submitted multiple times, even if only one change occurs to the file content. This is because other attributes of the file are also changed when the file is saved.
Created -- this event is submitted when a file is created in the monitored directory. If you plan to use this event to move the newly created event, you must write some error processing in the event processor.CodeIt can process the current file used by other processes. This is because the created event may be submitted before the file creation process releases the file. If you are not prepared to correctly handle this situation, an exception may occur.
Deleted -- submit this event when a file in the monitored directory is deleted.
Renamed -- submit this event when a file in the monitored directory is renamed.
Note: If you do not set enableraisingevents to true, the system does not submit any event. If the filesystemwatcher object sometimes does not seem to work, check enableraisingevents first to make sure it is set to true.

Event Processing

When filesystemwatcher calls an event processor, it contains two independent variables: an object called "sender" and a filesystemeventargs object called "E. The independent variable we are interested in is the filesystemeventargs independent variable. This object contains the reason for submitting the event. The following are some attributes of the filesystemeventargs object:

Attribute:

Name -- name of the file in which the event is submitted. It does not contain the file path-it only contains the file or directory name submitted using the event.
Changetype -- this is a watcherchangetypes instance that specifies the type of event to be submitted. Valid values include changed, created, deleted, and renamed.
Fullpath -- this attribute contains the complete path of the file for the event to be submitted, including the file name and directory name.

The filesystemwatcher object completes directory monitoring for you. If you create, update, or delete a file, filesystemwatcher submits an event to notify you of a change. In this way, after creating a new file, yourProgramYou can use this file immediately. Immediate notice of changes makes your system work more efficiently, because you cannot always "investigate" Changes in the directory, and there will be no time loss between two directory scans.

 

Example:

Class program {static void main (string [] ARGs) {watcherstrat (@ "D: \ spring \ Program ","*. * "); console. readkey ();} Private Static void watcherstrat (string path, string filter) {filesystemwatcher watcher = new filesystemwatcher (); watcher. path = path; watcher. filter = filter; watcher. changed + = new filesystemeventhandler (onprocess); watcher. created + = new filesystemeventhandler (onprocess); watcher. deleted + = new filesystemeventhandler (onprocess); watcher. renamed + = new renamedeventhandler (onrenamed); watcher. enableraisingevents = true; watcher. policyfilter = policyfilters. attributes | policyfilters. creationtime | policyfilters. directoryname | policyfilters. filename | policyfilters. lastaccess | policyfilters. lastwrite | policyfilters. security | policyfilters. size; watcher. includesubdirectories = true;} Private Static void onprocess (Object source, filesystemeventargs e) {If (E. changetype = watcherchangetypes. created) {oncreated (source, e);} else if (E. changetype = watcherchangetypes. changed) {onchanged (source, e);} else if (E. changetype = watcherchangetypes. deleted) {ondeleted (source, e) ;}} Private Static void oncreated (Object source, filesystemeventargs e) {console. writeline ("file creation event processing logic {0} {1} {2}", E. changetype, E. fullpath, E. name);} Private Static void onchanged (Object source, filesystemeventargs e) {console. writeline ("file change event processing logic {0} {1} {2}", E. changetype, E. fullpath, E. name);} Private Static void ondeleted (Object source, filesystemeventargs e) {console. writeline ("File Deletion event processing logic {0} {1} {2}", E. changetype, E. fullpath, E. name);} Private Static void onrenamed (Object source, renamedeventargs e) {console. writeline ("file rename event processing logic {0} {1} {2}", E. changetype, E. fullpath, E. name );}}

 

 

Running result

Summary

1. renaming triggers two events: renamed and changed.

2. Does filesystemwatcher have the same effect on NTFS and FAT32 disks?

 

 

Author: Spring Yang

Source: http://www.cnblogs.com/springyangwc/

The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent andArticleThe original text connection is clearly displayed on the page. Otherwise, the legal liability is retained.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.