Real-time file monitoring using filewatcher

Source: Internet
Author: User

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

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.