FileSystemWatcher Control Main Features:
Monitors the creation, deletion, alteration, renaming, and other activities of files in the specified file or directory. You can dynamically define the types of file types and file property changes that need to be monitored.
1. Several basic properties are commonly used:
(1) Path: Sets the path of the directory to be monitored.
(2) IncludeSubdirectories: Sets whether the cascade monitors subdirectories in the specified path.
(3) Filter: Sets a filter string that determines which types of files are monitored in the directory.
(4) NotifyFilter: Setting which properties of the file will trigger the changed event, while monitoring multiple property changes can be combined by "or". (The default value is Notifyfilter.lastwrite | Notifyfilter.filename | Notifyfilter.directoryname combination)
Subkey:Attributes--The properties of a file or folder.CreationTime--The time the file or folder was created. DirectoryName--Directory name. Common FileName--file name. (Common) Span style= "color: #990000;" >lastaccess -- the date on which the file or folder was last opened. lastwrite -- The last date the content was written to a file or folder. security security settings for-- files or folders. size -- the size of the file or folder. (common)
(5) EnableRaisingEvents: Set whether to start monitoring. (default = False)
2. Common events:
(1) Changed: Occurs when changing files and directories, you can set the properties that trigger the event to be changed by the NotifyFilter property.
(2) Created: Occurs when files and directories are created.
(3) Deleted: Occurs when a file or directory is deleted.
(4) Renamed: Occurs when renaming a file or directory.
(5) FileSystemEventArgs object:
Member: Name: Gets the names of the affected files or directories. Note: If it is a cascading monitoring subdirectory, the value is the path from the next directory in the monitoring directory to the affected file, not just the affected file name.
FullPath: Gets the fully qualified path of the affected file or directory.
changetype: Gets the type of event that occurs for the affected file or directory.
Subkey:all --creation of a file or folder , delete, change, or rename. changed --changes to a file or folder. The types of changes include size, properties, security settings, recent write times, and recent access time changes. created --Create a file or folder. deleted -- the deletion of files or folders. Renamed -- file or folder rename.
(6) RenamedEventArgs object:
Member:name: Gets the new name of the affected file or directory.
oldname: Gets the old name of the affected file or directory.
FullPath: Gets the fully qualified path of the affected file or directory.
Oldfullpath: Gets the previous fully qualified path of the affected file or directory.
changetype: Gets the type of event that occurs for the affected file or directory.
Subkey:all --creation of a file or folder , delete, change, or rename. Changed--Changes to the file or folder. The types of changes include size, properties, security settings, recent write times, and recent access time changes. Created--creation of a file or folder. Deleted--Deletion of files or folders. Renamed--Renaming a file or folder.
01.private delegate void Setlogtextdelegate (FileSystemEventArgs e); Declares a delegate that passes a FileSystemEventArgs object to update the UI interface for file created,deleted and changed changes. .03.private delegate void Renameddelegate (RenamedEventArgs e); Declares a delegate that passes a RenamedEventArgs object, and updates the UI interface when used for file renamed. 05.FileSystemWatcher fsw= new FileSystemWatcher (); .07.fsw. Path = "c:\\"; Set up the monitored Files directory 08. 09.FSW. IncludeSubdirectories = true; Set up all subdirectories under the monitor C-drive directory 10. 11.FSW. Filter = "*.txt|*.doc|*.jpg"; Set the type of monitoring file 12. 13.FSW. NotifyFilter = Notifyfilters.filename | Notifyfilters.directoryname | Notifyfilters.size; Setting file name, directory name, and file size changes will trigger changed event 14. 15.FSW. Created + = new FileSystemEventHandler (this.filesystemwatcher_eventhandle); A method that processes data after a bound event is triggered. 17.FSW. Deleted + = new FileSystemEventHandler (this.filesystemwatcher_eventhandle); 19.FSW. Changed + = new FileSystemEventHandler (this.filesystemwatcher_eventhandle); 21.FSW. Renamed + = new Renamedeventhandler (this.filesystemwatcher_renamed); Renaming an event is not the same as the parameter that is passed by the redaction.23.FSW. EnableRaisingEvents = true; Start monitoring 24. 25.private void Filesystemwatcher_eventhandle (object sender, FileSystemEventArgs e)//file additions and deletions are called when processing method 26. {. if (this.listView1.InvokeRequired)//Determine if cross thread 28. {This.listView1.Invoke (new setlogtextdelegate (Setlogtext), new object[] {e}); Marshals a method to the UI main thread processing 30 using a delegate. } 31.} 33.private void Filesystemwatcher_renamed (Object Sender,renamedeventargs e)//file is renamed when processing method 34 is called. {this.listView1.InvokeRequired)//Determine if cross thread 36. {PNS This.listView1.Invoke (new Renameddelegate (Setrenamedlogtext), new object[] {e}); Marshals a method to the UI main thread processing 38 using a delegate. } 39.} 41.private void Setlogtext (FileSystemEventArgs e)//Update UI Interface 42. {ListViewItem LVI = new ListViewItem (); 44.45. Lvi. SubItems.Add (e.name); The affected file name is 46. Lvi. SubItems.Add (E.changetype.tostring ()); The change type of the affected file (possibly created, Changed, Deleted) 48. Lvi. SubItems.Add (E.fullpath); The affected file full path 50. Wuyi This.listView1.Items.Add (LVI); 52.} 53. A. private void Setrenamedlogtext (RenamedEventArgs e)//Update UI Interface 55. {ListViewItem LVI = new ListViewItem (); 57.58. Lvi. SubItems.Add (E.oldname); The original of the affected file is 59. Lvi. SubItems.Add (E.changetype.tostring ()); Changes to the affected file type (Rename) 61. Lvi. SubItems.Add (e.name); The new name of the affected file is 63. Lvi. SubItems.Add (E.oldfullpath); The original path of the affected file is 65. Lvi. SubItems.Add (E.fullpath); The full path to the affected file (in fact, as in the original path) 67. This.fileeventlog_lvw. Items.Add (LVI); 69.70. }
Source: http://blog.csdn.net/hwt0101/article/details/8469285
FileSystemWatcher usage Explanation