Watching a File in C#
CoderSource.net
Watching a File in C# - Article by ra00l
Level: MediumType: Article
Rating: 4Page: 2 of 2

Date: 6/21/2005 12:00:00 AM

Environment: .Net, Windows, C#

   You should be careful when watching a large amount of files, and change a attribute that will affect most of them.

You will risk a buffer overflow and all the events raised will be lost. You can prevent this from happening by increasing the size of the buffer through the InternalBufferSize property,but this can seriously affect your application's performance.

   The FileSystemNotification class also has few events for handling the events raised. These actions are:

  • Changed - raised when changes are made to the size, system attributes, last write time, last access time, or security permissions of a file or directory in the directory being monitored
  • Created - raised when a new file/directory is created or a file/directory is copied or moved into the watched directory
  • Deleted - raised when a file or a directory is deleted
  • Renamed - raised when a file/directory is renamed
  • Error - raised when a buffer overflow error occurs

   Along with this article, you have a simple application that prints the actions you've taken to a certain directory.

Interface Screen

   Below, you have the simple way to set up a FileSystemWatcher. Note that tb_path and tb_type are textboxes, where you can select the path and type of files to watch.

watch.Path = tb_path.Text;

  watch.Filter = tb_type.Text;

  watch.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.LastWrite;

  watch.EnableRaisingEvents = true ;

  After setting up the FileSystemWatcher class, you need to handle the event raised by it. The simple way you can do that is by selecting the FileSystemWatecher object and go to Properties -> Events and selecting the event you want to take custom actions when it occurs.

         Here is the main window of the application in action:

Final Output

 


Attachments

Project Files File Watcher

1 2

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us