C # uses FileSystemWatcher to monitor the operation of files

Source: Internet
Author: User
Tags error handling

Reprint: http://www.cnblogs.com/cssmystyle/archive/2011/07/29/2120799.html

When planning a feature implementation, I want to implement a program has been running in the background, and then can monitor the user to some kind of suffix file operation, this thought needs to use the low-level code to write, in fact, otherwise, in the investigation process found a call FileSystemWatcher class can achieve my needs. The following is mainly about FileSystemWatcher related knowledge. FileSystemWatcher object for you to complete the directory monitoring work. If you create a new, updated, or deleted file, FileSystemWatcher will submit an event informing you that a change has occurred. Before applying the FileSystemWatcher object, you must understand some of the basic properties and events of the object. There is no doubt that the most important attribute of this object is the "EnableRaisingEvents" attribute. This property determines whether an object submits an event when it receives a change notification. If the EnableRaisingEvents property is set to False, the object will not commit a change event. If set to true, it commits a change event. Here are some other important attributes/events that you will use when applying the FileSystemWatcher object: properties:
path--This property tells FileSystemWatcher which path it needs to monitor. For example, if we set this property to "C:\Temp", the object monitors all changes that occur in that directory.
includesubdirectories--This property indicates whether the FileSystemWatcher object should monitor the changes that occur in subdirectories.
filter--This property allows you to filter out the changes that occur in certain types of files. For example, if we only want to submit a notification when a TXT file is modified/new/deleted, you can set this property to "*txt". This property is handy when dealing with high traffic or large directories. notifyfilter--Gets or sets the type of change to monitor.
enableraisingevents--Gets or sets a value that indicates whether this component is enabled.
includesubdirectories--Gets or sets a value that indicates whether to monitor subdirectories in the specified path. Event:
changed--This event is submitted when a file in the monitored directory is modified. It is noteworthy that this event may be submitted several times, even if the contents of the file are only one change. This is because other properties of the file also change when you save the file.
created--the event is submitted when a new file is created for the monitored directory. If you plan to use this event to move the newly created event, you must write some error handling code in the event handler that can handle the situation where the current file is being used by another process. This is done because the created event may be committed before the process that created the file releases the file. If you are not prepared to handle this situation correctly, you may receive an exception.
deleted--This event is submitted when a file in the monitored directory is deleted.
renamed--This event is submitted when a file in the monitored directory is renamed. When FileSystemWatcher calls an event handler, it contains two arguments-an object called "sender" and a FileSystemEventArgs object called "E". The independent variables we are interested in are filesystemeventargs. This object contains the reason for the commit event. The following are some properties of the FileSystemEventArgs object:

Property:
name--the name of the file in this property that causes the event to be committed. It does not contain a path to the file--contains only the file or directory name that was committed using the event.
changetype--This is a watcherchangetypes that indicates which type of event to commit. Its valid values include: Changed Created Deleted renamed

fullpath--This property contains the full path to the file that causes the event to be committed, including the file name and directory name. A simple example of an application of the FileSystemWatcher object is listed below. In this example, we monitor the operation of *.txt in the "d:\tmp" directory.

[PermissionSet (SecurityAction.Demand, Name = "FullTrust")]
private void Watcher ()
{
	FileSystemWatcher watcher=new FileSystemWatcher ();
	Watcher. Path = @ "d:\tmp";
	Watcher. Filter = "*.txt";
	Watcher. EnableRaisingEvents = true;//Open Commit event
	watcher. IncludeSubdirectories = true;//allows detection of subdirectory watcher under this directory
	. NotifyFilter = Notifyfilters.filename;
	Watcher. Changed + = onchanged;
	Watcher. Created + = onchanged;
	Watcher. Deleted + = onchanged;
	Watcher. Renamed + + onrenamed;
}
private static void OnChanged (object source, FileSystemEventArgs e)
{
	Console.WriteLine ("File:" + E.fullpath + "" + e.ChangeType);
}
private static void Onrenamed (object source, RenamedEventArgs e)
{
	Console.WriteLine ("File: {0} renamed to {1}") ", E.oldfullpath, E.fullpath);
}

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.