C # FileSystemWatcher instructions for using

Source: Internet
Author: User


FileSystemWatcher Control Main Features:
Monitors the creation, deletion, alteration, renaming, and other activities of files in a specified file or directory. You can dynamically define the type of file you want to monitor and the types of file attribute changes.

1. Several basic properties commonly used:
(1) Path: Sets the paths to the directories to be monitored.

(2) IncludeSubdirectories: Sets whether cascading 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: The change of which properties of the set file triggers the changed event, while monitoring multiple property changes can be grouped 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--filename. Common
Lastaccess-the date on which the file or folder was last opened.
Lastwrite-the date on which the content was last written to a file or folder.
Security-Secure settings for files or folders.
Size-The file or folder sizes. Common

(5) EnableRaisingEvents: Set whether to start monitoring. (Default is False)

2. Common events:
(1) Changed: Occurs when you change files and directories, and you can set properties that trigger this event that require file changes by setting 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 name of the affected file or directory. Note: If cascading monitors subdirectories, this value is the path from the next directory in the monitor 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 occurred for the affected file or directory.

Subkeys: All-Create, delete, change, or rename a file or folder.
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-the creation of a file or folder.
Deleted-Deletes a file or folder.
Renamed-Rename a file or folder.

(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 previously fully qualified path of the affected file or directory.

changetype: Gets the type of event that occurred for the affected file or directory.

Subkeys: All-Create, delete, change, or rename a file or folder.
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-the creation of a file or folder.
Deleted-Deletes a file or folder.
Renamed-Rename a file or folder.


Instance

The code is as follows Copy Code

Private delegate void Setlogtextdelegate (FileSystemEventArgs e); Declares a delegate that passes the FileSystemEventArgs object to update the UI interface for file created,deleted and changed changes.

Private delegate void Renameddelegate (RenamedEventArgs e); Declares a delegate that passes the RenamedEventArgs object and updates the UI interface for file renamed.

FileSystemWatcher fsw= New FileSystemWatcher ();

Fsw.   Path = "C:\"; Set up a monitored file directory

Fsw.   IncludeSubdirectories = true; Set up all subdirectories in the monitor C disk directory

Fsw.   Filter = "*.txt|*.doc|*.jpg"; To set the type of monitoring file

Fsw. NotifyFilter = Notifyfilters.filename | Notifyfilters.directoryname |   Notifyfilters.size; Setting the file name, directory name, and file size changes trigger the changed event

Fsw.  Created + = new FileSystemEventHandler (this.filesystemwatcher_eventhandle); A method of processing data after a binding event is triggered.

Fsw. Deleted + = new FileSystemEventHandler (this.filesystemwatcher_eventhandle);

Fsw. Changed + = new FileSystemEventHandler (this.filesystemwatcher_eventhandle);

Fsw.  Renamed + = new Renamedeventhandler (this.filesystemwatcher_renamed); The Rename event is not the same as the parameter being passed by the deletion.

Fsw.  EnableRaisingEvents = true; Start monitoring

private void Filesystemwatcher_eventhandle (object sender, FileSystemEventArgs e)//file additions and deletions modified by the processing method
{
if (this.listView1.InvokeRequired)//To determine whether cross threading
{
This.listView1.Invoke (New Setlogtextdelegate (Setlogtext), new object[] {e}); Marshaling a method by using a delegate to the UI main thread processing
}
}

Handling method invoked when private void Filesystemwatcher_renamed (Object Sender,renamedeventargs e)//File Rename
{
if (this.listView1.InvokeRequired)//To determine whether cross threading
{
This.listView1.Invoke (New Renameddelegate (Setrenamedlogtext), new object[] {e}); Marshaling a method by using a delegate to the UI main thread processing
}
}

private void Setlogtext (FileSystemEventArgs e)//Update UI interface
{
ListViewItem LVI = new ListViewItem ();

Lvi.   SubItems.Add (e.name); The file name being affected

Lvi.   SubItems.Add (E.changetype.tostring ()); Types of changes to affected files (may be created, Changed, Deleted)

Lvi.     SubItems.Add (E.fullpath); Affected file Full path

THIS.LISTVIEW1.ITEMS.ADD (LVI);
}

private void Setrenamedlogtext (RenamedEventArgs e)//Update UI interface
{
ListViewItem LVI = new ListViewItem ();

Lvi.   SubItems.Add (E.oldname); The original of the affected document

Lvi.  SubItems.Add (E.changetype.tostring ()); Type of change of affected file (Rename)

Lvi.   SubItems.Add (e.name); The new name of the affected file

Lvi.     SubItems.Add (E.oldfullpath); The original path of the affected file

Lvi.  SubItems.Add (E.fullpath); The full path of the affected file (in fact, same as the original path)

This.fileeventlog_lvw. Items.Add (LVI);

}

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 is passing the Renameeventargs object, while creating, modifying, and deleting events is passing the Fileeventargs object, so you need to declare two delegates.

3, if you need to monitor multiple files simultaneously (such as monitoring system overall), just create FileSystemWatcher array, each file with a FileSystemWatcher for monitoring.

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.