The examples in this article detail the FileSystemWatcher usage of C #. Share to everyone for your reference. The specific usage is as follows:
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-when the file or folder was created.
directoryname-directory name. Common
filename-file name. Common
lastaccess-the date that the file or folder was last opened.
lastwrite-the last date the content was written to a file or folder.
security-the security settings for a file or folder.
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-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-the deletion of a file or folder.
Rename the renamed-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 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 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-the deletion of a file or folder.
Rename the renamed-file or folder.
Instance:
The code is as follows:
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.
Private delegate void Renameddelegate (RenamedEventArgs e); Declares a delegate that passes a RenamedEventArgs object, and updates the UI interface when used for file renamed.
FileSystemWatcher fsw= new FileSystemWatcher ();
Fsw. Path = "C: \"; Set up the monitored files directory
Fsw. IncludeSubdirectories = true; Set up all subdirectories under monitor C-Drive directory
Fsw. Filter = "*.txt|*.doc|*.jpg"; Set the type of monitoring file
Fsw. NotifyFilter = Notifyfilters.filename | Notifyfilters.directoryname | Notifyfilters.size; Setting file name, directory name, and file size changes will trigger the changed event
Fsw. Created + = new FileSystemEventHandler (this.filesystemwatcher_eventhandle); A method that processes data after a bound event is triggered.
Fsw. Deleted + = new FileSystemEventHandler (this.filesystemwatcher_eventhandle);
Fsw. Changed + = new FileSystemEventHandler (this.filesystemwatcher_eventhandle);
Fsw. Renamed + = new Renamedeventhandler (this.filesystemwatcher_renamed); Renaming an event is not the same as the parameter that is passed by the redaction.
Fsw. EnableRaisingEvents = true; Start monitoring
private void Filesystemwatcher_eventhandle (object sender, FileSystemEventArgs e)//file additions and deletions are called processing methods
{
if (this.listView1.InvokeRequired)//Determine if cross-threading
{
This.listView1.Invoke (New Setlogtextdelegate (Setlogtext), new object[] {e}); To marshal a method to the UI main thread processing using a delegate
}
}
private void Filesystemwatcher_renamed (Object Sender,renamedeventargs E)//The processing method that is called when the file is renamed
{
if (this.listView1.InvokeRequired)//Determine if cross-threading
{
This.listView1.Invoke (New Renameddelegate (Setrenamedlogtext), new object[] {e}); To marshal a method to the UI main thread processing using a delegate
}
}
private void Setlogtext (FileSystemEventArgs e)//Update UI interface
{
ListViewItem LVI = new ListViewItem ();
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)
Lvi. SubItems.Add (E.fullpath); The full path of the affected file
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 file
Lvi. SubItems.Add (E.changetype.tostring ()); Types of changes to affected files (Rename)
Lvi. SubItems.Add (e.name); 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 (as in fact the original path)
This.fileeventlog_lvw. Items.Add (LVI);
}
Focus:
1, because the FileSystemWatcher class itself is a multithreaded control, that is, every 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
I hope this article is helpful to everyone's C # programming.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
Examples of FileSystemWatcher usages in C #
This address: http://www.paobuke.com/develop/c-develop/pbk23326.html
Related content ftphelper implement FTP server file read and write operations (C #)? ê?ac# socket±à3ì±ê?? C # console Basic list initialization two methods a simple instance of writing a Bluetooth communication program in Windows system using C #
Methods for finding duplicate values in dictionary in C # use WinRAR in C # to implement cryptographic compression and Extract files anonymous method instance resolution in C # efficient C # Coding optimization Principles
Examples of FileSystemWatcher usages in C #