This article mainly introduces C # using FileSystemWatcher control to implement the file monitoring function, combined with instance form analysis of C # FileSystemWatcher components of the function and monitoring file changes of the specific use of skills, the need for friends can refer to the next
This example describes the file monitoring capabilities that C # uses with FileSystemWatcher controls. Share to everyone for your reference, as follows:
FileSystemWatcher
You can use the FileSystemWatcher component to monitor the file system and respond to changes to the file system. By using the FileSystemWatcher component, a business process can be started quickly and easily when a particular file or directory is created, modified, or deleted.
For example, if a group of users is working on a document that is stored in a server shared directory, you can use the FileSystemWatcher component to write an application to monitor changes to the shared directory. When a change is detected, the component can run the processing process and notify each user by e-mail.
You can configure components to monitor the entire directory and its contents, or a specific file or set of files in a specific directory. To monitor changes in all files, set the Filter property to an empty string (""), or to monitor a specific file, set the Filter property to the file name of the file (for example, to monitor changes in file MyDoc.txt, set the Filter property to " MyDoc.txt "), or you can monitor changes in a specific file type, such as to monitor changes in a text file, set the Fillter property to" *.txt ".
"Tip" do not ignore hidden files.
You can monitor several kinds of changes in a directory or file. For example, you can monitor the attributes of a file or directory, the date and time of lastwrite, or the change in size. By adding
The Filesystemwatcher.notifyfilter property is set to a value in notifyfilters to achieve this goal.
You can also monitor the renaming, deletion, or creation of files or directories. For example, to monitor the renaming of a text file, you can set the Filter property to "*.txt", call a WaitForChanged method, and set the value of WatcherChangeTypes in the method to renamed.
The following examples illustrate. Create a Filesystemwatche component to monitor the directory specified at run time. Components are set up to monitor changes in Lastwrite and lastaccess time, as well as the creation, deletion, or renaming of text files in the directory. If the file is changed, created, or deleted, the path to the file is output to the console. When a file is renamed, both the old and the new paths are output to the console.
An instance uses the Filesystemwatche component to monitor the directory specified at run time:
Using system;using System.io;public class Watcher {public static void Main (string[] args) {//If no directory is specified, exit the program if ( Args. Length!=1) {//Display the correct method for calling the program Console.WriteLine ("Usage:Watcher.exe (Directory)"); Return }//Create a new FileSystemWatcher and set its properties FileSystemWatcher watcher=new FileSystemWatcher (); Watcher. Path=args[o]; /* Monitor changes in lastaccess and Lastwrite time and the renaming of files or directories */Watcher. notifyfilter=notifyfilters.lastaccess | Notifyfilters.lastwrite | Notifyfilters.filename | Notifyfilters.directoryname; Only the text file watcher is monitored. Filter= "*.txt"; Add an event handle//The Change event occurs when the//size, System properties, last write time, last access time, or security permissions//of the file or directory in the path specified by FileSystemWatcher is changed watcher. Changed +=new FileSystemEventHandler (OnChanged); When a file or directory is created in the path specified by FileSystemWatcher, watcher occurs when the event is created. Created +=new FileSystemEventHandler (OnChanged); When a file or directory in the path specified by FileSystemWatcher is deleted, the Delete event occurs watcher. Deleted +=new FileSystemEventHandler (OnChanged); When a file or directory is renamed in the path specified by FileSystemWatcher, watcher occurs when you rename an event. Renamed +=new Renamedeventhandler (onrenamed); Start monitoring watcher. Enableraisingevents=true; Wait for the user to exit the program Console.WriteLine ("Press\ ' q\ ' to quit the sample."); while (Console.read ()! = ' Q '); }//define event handler public static void OnChanged (Object Sender,filesystemeventargs e) {//Specify what to do when a file is changed, created, or deleted CONSOLE.W Riteline ("File:" +e.fullpath+ "" +e.changetype); public static void Onrenamed (Object Sender,renamedeventargs e) {//Specifies the action that occurs when a file is renamed Console.WriteLine ("Fi]e:{0} Renamed To{1} ", E.oldfullpath,e.fullpath); }}