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
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--a file or folder's safety settings.
Size-a 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 -Create, delete, change, or rename a file or folder.
Changed -Changes to files or folders. The types of changes include size, properties, security settings, recent write times, and recent access time changes. &NBSP
created & nbsp; --creation of a file or folder.
Deleted -- file or folder deletion.
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 -Create, delete, change, or rename a file or folder.
Changed -Changes to files or folders. The types of changes include size, properties, security settings, recent write times, and recent access time changes. &NBSP
created & nbsp; --creation of a file or folder.
Deleted -- file or folder deletion.
Renamed -- file or folder rename.
Demo Source:
1 Private Delegate voidSetlogtextdelegate (FileSystemEventArgs e);//declares a delegate that passes a FileSystemEventArgs object to update the UI interface for file created,deleted and changed changes. 2 3 Private Delegate voidRenameddelegate (RenamedEventArgs e);//declares a delegate that passes a RenamedEventArgs object, and updates the UI interface when used for file renamed. 4 5FileSystemWatcher fsw=NewFileSystemWatcher ();6 7Fsw. Path = "c:\\";//set up the monitored files directory8 9Fsw. IncludeSubdirectories =true;//set up all subdirectories under monitor C-Drive directoryTen OneFsw. Filter ="*.txt|*.doc|*.jpg";//set the type of monitoring file A -Fsw. NotifyFilter = Notifyfilters.filename | Notifyfilters.directoryname | Notifyfilters.size;//setting file name, directory name, and file size changes will trigger the changed event - theFsw. Created + =NewFileSystemEventHandler ( This. Filesystemwatcher_eventhandle);//a method that processes data after a bound event is triggered. - -Fsw. Deleted + =NewFileSystemEventHandler ( This. Filesystemwatcher_eventhandle); - +Fsw. Changed + =NewFileSystemEventHandler ( This. Filesystemwatcher_eventhandle); - +Fsw. Renamed + =NewRenamedeventhandler ( This. filesystemwatcher_renamed);//renaming an event is not the same as the parameter that is passed by the redaction. A atFsw. EnableRaisingEvents =true;//Start Monitoring - - Private voidFilesystemwatcher_eventhandle (Objectsender, FileSystemEventArgs e)//the processing method that is called when the file is changed or deleted - { - if( This. listview1.invokerequired)//determine if cross-threading - { in This. Listview1.invoke (NewSetlogtextdelegate (Setlogtext),New Object[] {e});//To marshal a method to the UI main thread processing using a delegate - } to } + - Private voidFilesystemwatcher_renamed (ObjectSender,renamedeventargs e)//the processing method that is called when the file is renamed the { * if( This. listview1.invokerequired)//determine if cross-threading $ { Panax Notoginseng This. Listview1.invoke (NewRenameddelegate (Setrenamedlogtext),New Object[] {e});//To marshal a method to the UI main thread processing using a delegate - } the } + A Private voidSetlogtext (FileSystemEventArgs e)//updating the UI interface the { +ListViewItem LVI =NewListViewItem (); - $Lvi. SubItems.Add (e.name);//the affected file name $ -Lvi. SubItems.Add (E.changetype.tostring ());//types of changes to affected files (may be created, Changed, Deleted) - theLvi. SubItems.Add (E.fullpath);//the full path of the affected file - Wuyi This. LISTVIEW1.ITEMS.ADD (LVI); the } - Wu Private voidSetrenamedlogtext (RenamedEventArgs e)//updating the UI interface - { AboutListViewItem LVI =NewListViewItem (); $ -Lvi. SubItems.Add (E.oldname);//The original of the affected file - -Lvi. SubItems.Add (E.changetype.tostring ());//types of changes to affected files (Rename) A +Lvi. SubItems.Add (e.name);//new name of the affected file the -Lvi. SubItems.Add (E.oldfullpath);//The original path of the affected file $ theLvi. SubItems.Add (E.fullpath);//The full path of the affected file (as in fact the original path) the the This. Fileeventlog_lvw. Items.Add (LVI); the -}
View Code
Focus:
1. Because the FileSystemWatcher class itself is a multithreaded control, that is, each instantiation of a FileSystemWatcher automatically creates a thread.
2, we need to use the delegate to update the UI interface across threads, because the renaming event passes the Renameeventargs object, and the Create, modify, delete event passes the Fileeventargs object, so you need to declare two delegates.
3, if you need to monitor multiple files simultaneously (such as monitoring system overall), only need to create FileSystemWatcher array, each file with a FileSystemWatcher for monitoring.
Instance effect:
Now put on the above-mentioned C # file monitoring source code, I hope to learn from you to help:
Source: http://download.csdn.net/detail/czw2010/4912798
This article transferred from: http://blog.csdn.net/czw2010/article/details/7916262
FileSystemWatcher usage Explanation