Filewatcher can monitor files (new, renamed, content modified, and deleted) in a directory in real time.
Using System;
Using System. IO;
Using System. Windows. forms;
Namespace FW
{
Public Partial Class Frm1: Form
{
Private Filesystemwatcher watcher;
Private Delegate Void Updatewatchtextdelegate ( String Newtext );
Public Frm1 ()
{
Initializecomponent ();
This . Watcher = New Filesystemwatcher ();
This . Watcher. Deleted + = New Filesystemeventhandler (watcher_deleted );
This . Watcher. Renamed + = New Renamedeventhandler (watcher_renamed );
This . Watcher. Changed + = New Filesystemeventhandler (watcher_changed );
This . Watcher. Created + = New Filesystemeventhandler (watcher_created );
}
Public VoidUpdatewatchtext (StringNewtext)
{
Lblwatch. Text=Newtext;
}
Public Void Writelog ( String Logcontent)
{
Using (Streamwriter SW = New Streamwriter ( " C: \ log.txt " , True ))
{
Sw. writeline (logcontent );
Sw. Close ();
}
}
Void Watcher_created ( Object Sender, filesystemeventargs E)
{
Try
{
Writelog (string. Format ( " File: {0} created " , E. fullpath ));
This . Begininvoke ( New Updatewatchtextdelegate (updatewatchtext ), " File " + E. fullpath + " Created " );
}
Catch (Ioexception)
{
This . Begininvoke ( New Updatewatchtextdelegate (updatewatchtext ), " An error occurred while creating log writing! " );
}
}
Void Watcher_changed ( Object Sender, filesystemeventargs E)
{
Try
{
Writelog (string. Format ( " File: {0} {1} " , E. fullpath, E. changetype. tostring ()));
This . Begininvoke ( New Updatewatchtextdelegate (updatewatchtext ), " File " + E. fullpath + " Modified " );
}
Catch (Ioexception)
{
This . Begininvoke ( New Updatewatchtextdelegate (updatewatchtext ), " Failed to modify log writing! " );
}
}
Void Watcher_renamed ( Object Sender, renamedeventargs E)
{
Try
{
Writelog (string. Format ( " File renamed from {0} to {1} " , E. oldname, E. fullpath ));
This . Begininvoke ( New Updatewatchtextdelegate (updatewatchtext ), " File " + E. oldname + " Renamed " + E. fullpath );
}
Catch (Ioexception)
{
This . Begininvoke ( New Updatewatchtextdelegate (updatewatchtext ), " An error occurred while writing the renamed log! " );
}
}
Void Watcher_deleted ( Object Sender, filesystemeventargs E)
{
Try
{
Writelog (string. Format ( " File: {0} deleted " , E. fullpath ));
This . Begininvoke ( New Updatewatchtextdelegate (updatewatchtext ), " File " + E. fullpath + " Deleted " );
}
Catch (Ioexception)
{
This . Begininvoke ( New Updatewatchtextdelegate (updatewatchtext ), " Failed to delete log writing! " );
}
}
Private Void Cmdbrowse_click ( Object Sender, eventargs E)
{
If ( This . Folderbrowserdialog1.showdialog () ! = Dialogresult. Cancel)
{
Txtlocation. Text = This . Folderbrowserdialog1.selectedpath;
Cmdwatch. Enabled = True ;
}
}
Private Void Cmdwatch_click ( Object Sender, eventargs E)
{
If (Txtlocation. Text. Length <= 0 )
{
MessageBox. Show ( " Select the folder to be monitored first! " );
Cmdbrowse. Focus ();
Return ;
}
Watcher. Path = Txtlocation. text; // Monitoring path (folder)
Watcher. Filter = " *.* " ; // If the filter is a file name, the file is monitored. If the filter is * .txt, all. txt files in the specified directory must be monitored.
Watcher. policyfilter = Policyfilters. lastwrite |
Policyfilters. filename |
Yyfilters. size;
Lblwatch. Text = Watcher. Path + " Monitoring " ;
//Begin watching.
Watcher. enableraisingevents= True;
}
Private Void Btnstop_click ( Object Sender, eventargs E)
{
Watcher. enableraisingevents = False ;
Lblwatch. Text = Watcher. Path + " Monitoring has stopped! " ;
}
}
}
Note: if there are subdirectories in the directory, filewatcher cannot monitor the files in the subdirectories by default. You can set watcher. includesubdirectories = true to solve this problem.Source code download: http://files.cnblogs.com/yjmyzz/FileWatcher.rar