FileSystemWatcher How to use

Source: Internet
Author: User

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. a few basic properties 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 void Setlogtextdelegate (FileSystemEventArgs e);  //Declares a delegate that passes the FileSystemEventArgs object for updating the UI interface for file created,deleted and changed changes.
  2. Private delegate void Renameddelegate (RenamedEventArgs e);  //Declares a delegate that passes the RenamedEventArgs object to update the UI interface when used for file renamed.
  3. FileSystemWatcher fsw= New FileSystemWatcher ();
  4. Fsw.   Path = "c:\\"; //Set the monitored files directory
  5. Fsw.   IncludeSubdirectories = true; //Set up all subdirectories under monitor C-Drive directory
  6. Fsw.   Filter = "*.txt|*.doc|*.jpg"; //Set the type of monitoring file
  7. Fsw. NotifyFilter = Notifyfilters.filename | Notifyfilters.directoryname |   Notifyfilters.size; //settings file filename, directory name, and file size changes will trigger the changed event
  8. Fsw.  Created + = new FileSystemEventHandler (this.filesystemwatcher_eventhandle);  //The method of processing data after a bound event is triggered.
  9. Fsw.  Deleted + = new FileSystemEventHandler (this.filesystemwatcher_eventhandle);
  10. Fsw.  Changed + = new FileSystemEventHandler (this.filesystemwatcher_eventhandle);
  11. Fsw.  Renamed + = new Renamedeventhandler (this.filesystemwatcher_renamed);  the//rename event is not the same as the parameter for the add-and-snip pass.
  12. Fsw.  EnableRaisingEvents = true; //Start monitoring
  13. Private void Filesystemwatcher_eventhandle (object sender, FileSystemEventArgs e) //file additions and deletions are called processing methods
  14. {
  15. if (this.listView1.InvokeRequired) //Determine if cross-threading
  16. {
  17. This.listView1.Invoke (new Setlogtextdelegate (Setlogtext), new object[] {e}); //Use a delegate to marshal the method to the UI main thread processing
  18. }
  19. }
  20. Private void filesystemwatcher_renamed (object Sender,renamedeventargs e) //The processing method that is called when the file is renamed
  21. {
  22. if (this.listView1.InvokeRequired) //Determine if cross-threading
  23. {
  24. This.listView1.Invoke (new Renameddelegate (Setrenamedlogtext), new object[] {e}); //Use a delegate to marshal the method to the UI main thread processing
  25. }
  26. }
  27. Private void Setlogtext (FileSystemEventArgs e) //Update UI interface
  28. {
  29. ListViewItem LVI = new ListViewItem ();
  30. Lvi.   SubItems.Add (e.name); //Affected file name
  31. Lvi.   SubItems.Add (E.changetype.tostring ()); types of changes to affected files (may be created, Changed, Deleted)
  32. Lvi.     SubItems.Add (E.fullpath); //affected file full path
  33. This.listView1.Items.Add (LVI);
  34. }
  35. private void Setrenamedlogtext (RenamedEventArgs e) //Update UI interface
  36. {
  37. ListViewItem LVI = new ListViewItem ();
  38. Lvi.   SubItems.Add (E.oldname); // The original of the affected file
  39. Lvi.  SubItems.Add (E.changetype.tostring ()); //Changes to the affected file type (Rename)
  40. Lvi.   SubItems.Add (e.name); //The new name of the affected file
  41. Lvi.     SubItems.Add (E.oldfullpath); //The original path of the affected file
  42. Lvi.  SubItems.Add (E.fullpath); //The full path of the affected file (as in fact the original path)
  43. THIS.FILEEVENTLOG_LVW.  Items.Add (LVI);
  44. }

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:

C # File monitoring source code, hope to learn from you to help:

Source: http://download.csdn.net/detail/czw2010/4912798

Reproduced from the original address: http://blog.csdn.net/czw2010/article/details/7916262

FileSystemWatcher How to use

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.