Analyze problems
The filesystemwatcher type is defined in the system. Io namespace. Its function is to monitor changes to a specific file system in a folder. Such changes can include creation, file modification, renaming, and deletion. Programmers can subscribe to information of interest by setting a member of The policyfilters Enumeration type. The following table lists all the items of policyfilters and their meanings.
Name |
Description |
Length |
Attributes |
File or folder attributes |
4 |
Creationtime |
File or folder Creation Time |
64 |
Directoryname |
Directory Name |
2 |
Filename |
File Name |
1 |
Lastaccess |
Date on which the last file or folder was opened |
32 |
Lastwrite |
Last date when content was written to a file or folder |
16 |
Security |
File or folder Security Settings |
256 |
Size |
File or folder size |
8 |
By setting folders and files of interest, changing types, and processing time, programmers can listen to the file system. The following code shows how to use filesystemwatcher.
Description
The event mechanism in. net will be covered in subsequent chapters of this book. Simply put, events represent a special type that can trigger callback methods.
Using system; using system. io; namespace test {class usefilesystemwatcher {// exit program command private const string exit = "exit"; // The monitored folder private const string folder = @ "D: \ test "; private filesystemwatcher _ FSW; Public usefilesystemwatcher () {_ FSW = new filesystemwatcher (); // create a monitoring folder if (directory. exists (folder) {directory. delete (folder, true);} directory. createdirectory (folder); _ FSW. path = folder; // here When selecting the last file access time, last file write time, file name, // folder name change, the event _ FSW is triggered. policyfilter = policyfilters. lastaccess | policyfilters. lastwrite | policyfilters. filename | policyfilters. directoryname; // click here to monitor all the ZIP files _ FSW. filter = "*. RAR "; // Add a processing method for these events. _ FSW. changed + = new filesystemeventhandler (onchanged); _ FSW. created + = new filesystemeventhandler (onchanged); _ FSW. deleted + = new filesystemeventhandler (onchanged); _ FSW. renamed + = new renamedeventhandler (onrenamed); // all events will be triggered here _ FSW. enableraisingevents = true;} // process change events. changes can include creating, modifying, and deleting private void onchanged (Object source, filesystemeventargs e) {console. writeline ("file: {0} {1}", E. fullpath, E. changet Ype. tostring ();} // process the rename event Private Static void onrenamed (Object source, renamedeventargs e) {console. writeline ("file: {0} renamed: {1}", E. oldfullpath, E. fullpath);} static void main () {try {usefilesystemwatcher Ws = new usefilesystemwatcher (); While (console. readline ()! = Exit) {}} finally {// clear test data if (directory. exists (folder) {directory. Delete (folder, true );}}}}}
The code above creates a filesystemwatcher object that monitors the D: \ test directory, and subscribes to events of interest such as modification, creation, and deletion. When the program is running, you can try to create, delete, and copy files under the D: \ test directory. The console will output the relevant information, finally, you can enter exit to end the running of this program.
The most common question about filesystemwatcher is why the changed event was triggered multiple times during file replication. This is not a. net bug, but a normal file system operation does contain multiple simple operations. For example, a mobile operation may involve modifying files multiple times and may create or delete files.
Note:
Some special monitoring software, such as firewall software, may cause the occurrence of filesystemwatcher monitoring events.
Although filesystemwatcher is easy to use, you still need to pay attention to some details when designing a system that uses filesystemwatcher. Filesystemwatcher maintains an internal buffer to receive modification notifications from the file system. However, when there are too many modifications to the file system in a short time, the buffer overflows, at this time, some modifications will be lost. Programmers can use internalbuffersize to modify the internal cache size.
Tips
I do not recommend that you increase the internal cache size of filesystemwatcher at will, because this has a huge impact on performance. I suggest you use other mechanisms to avoid loss of File Modification events, such as speeding up processing and reducing monitoring scope.
Answer
. NET provides the filesystemwatcher type for file system monitoring. By setting the monitoring directory, monitoring type, and callback method, programmers can conveniently implement the monitoring function. But be careful when filesystemwatcher cache overflow occurs.