Now: Tutorial for Web and Software Design > Programming > dotnet > Programming Content
> Watching a File in C# [Bookmark it]
Watching a File in C#

   This article talks about how to set a watch on a file or directory, and take custom actions when that file is modified.

These tasks can be accomplished by using the .NET class FileSystemWatcher. You can watch for changes in files and subdirectories of the specified directory. The component can watch files on a local computer, a network drive, or a remote computer.

   Let's go a little deeper into the class and find out more about it, by taking a look at its methods and properties. The first thing we need to do after instantiating the class is set its Path property to the path of the directory that we'd like to watch. If we'd like to watch a file and not a directory, we should set the Path property to the file's parent directory and the Filter property to the name of the file that we would like to watch.

   But what happens when we rename the directory? Would the FilsSystemWatcher still watch the old path? The answer is no. It will automatically watch the new directory's name. Still, if you look at the Path property, it would still hold the old name of the folder. The next important property is the Filter property that I've mentioned above. This property sets the file or type of file contained in the directory specified by Path to watch. The default value of the Filter property is “*.*”. This means that the FileSystemWatcher object will watch all files that have an extension, whatever the extension or the file name is. Consider that if you have a file called TEST, it would not be watched, since it doesn't respect the pattern “*.*”(it has no extension). To watch for a specific file, set the Filter property to that file's name(Ex.: “Test.txt”); if you would like to watch all files in that directory, set the Filter to “*”. Finally, if you would like to watch for files that have a certain extension, set the Filter to “*.[extension]”(Ex.: watch for text files - ”*.txt”).

   If you would like to watch also the subdirectories of the directory entered in Path, set the IncludeSubdirectories property to true.

   Also, you have an option to specify the type of change actions that will be watched. You can change them though the NotifyFilter property. The default types of changes are FileName (the name of the file), DirectoryName (the name of the directory) and LastWrite (last modified date). You can modify them by creating a combination of the others members of the NotifyFilters enumeration. These members are:

Attributes (raises an event when the folder's attributes are changed), CreationTime (watch the time the file/folder was created - raises an event when you create/copy a file into the directory), DirectoryName (raises an event when a directory is renamed), FileName (raises an event when a file is renamed), and LastAccess (raises an event when the file / folder is opened), LastWrite (raises an event when the file is modified),Security (raises an event when the file's or folder's security changed) and Size(raises an event when the file's size changes). Let's suppose that we have a FileSystemWatcher object on a directory and its Filter is the file “test.txt”. If we have all the NotifyFilters watching for changes and we will open the file, type something, save and close it. The LastWrite NotifyFilter will raise an event. But also, the Size filter will also raise an event. The same thing happens if you change the security of a folder. You will have all the files in that folder raising a Security event.


Attachments

Project Files File Watcher

1 2

[Bookmark][Print] [Close][To Top]
  • Prev Article-Programming:

  • Next Article-Programming:
  • Related Materias
    Using the For匛ach Stateme
    Creating a Toolbar - Visua
    Using Office applications 
    Visual Basic Explorer - Tu
    Visual Basic Explorer - Tu
    Visual Basic Explorer - Si
    Visual Basic Explorer- Gam
    Visual Basic Explorer- Gam
    Visual Basic Explorer- Gam
    Visual Basic Explorer- Gam
    Topics
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Graphic Design Tutorial
     

    Coreldraw Tutorial

      Illustrator Tutorial
      3D Graphics Articles
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial&Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial&Articles
     

    XML Style Tutorial

      AJAX Tutorial
      XML Mobile
    Flash Tutorial&Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial&Articles
     

    Linux Tutorial

      Symbian Tutorial
      MacOS Tutorial