Jump to content

Visual C# : File System Watcher (basics)

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Jarryd

Jarryd

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts
Difficulty: 2 / 10
Assumed Knowledge: Very little knowledge of C#.
Information: Listens to the file system notifications and raises events when a directory, or file in a directory, has changed, been created, renamed and even deleted.
requirements: fileSystemWatcher module.

Posted Image

Step 1: Adding functions
Form 1: - Load

            fileSystemWatcher1.Path = "C:/";

            fileSystemWatcher1.Filter = "*.*";


            fileSystemWatcher1.IncludeSubdirectories = true; 

            // Allows the file system watcher to search though sub firectories


            fileSystemWatcher1.EnableRaisingEvents = true;

            // Gets or sets a value indicating whether the component is enabled.
This sets the path and the filter to pick up certain file formats, by setting the subdirectories true the fileSystemWatcher will search though all sub directories, Enable raising event gets or sets a value indicating whether the component is enabled.

Step 2: adding unimplemented methods
private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)

        {

            listBox1.Items.Add("File (" + e.ChangeType + ") : " + e.FullPath);

            // Tells the user that something in the system has been changed.

        }


        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)

        {

            listBox1.Items.Add("File (" + e.ChangeType + ") : " + e.FullPath);

            // Tells the user that something in the system has been Created.

        }


        private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)

        {

            listBox1.Items.Add("File (" + e.ChangeType + ") : " + e.FullPath);

            // Tells the user that something in the system has been Deleted.

        }


        private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)

        {

            listBox1.Items.Add("File (" + e.ChangeType + ") : " + e.FullPath);

            // Tells the user that something in the system has been renamed.

        }
[Note: These methods may need to be written - not c&p]
These methods, will tell the user if a file is changed, created, renamed and even deleted.

#2
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Do you know the efficiency of this api? i.e. say you wrote app using fswatcher to back up data from a server. Now is there a max modification / size limit / rate that it would guarantee to work well.

I remember having written such an application (at least 4 years ago) where i had to back up data from a server. The back up machine was on a gigabit LAN so that was not an issue.

The data contains pictures (jpegs) and a lot of text files and was of course multi threaded. I remember it always worked well until i start modifying data on the server beyond a certain max size. i.e. if i copied a 2 MB folder from one place to another containing a few hundred files, it worked fine. But any thing beyond 2 MB, and it would start missing a whole lot of data.

If i copied 500 MB (containing thousands of pics), merely a few hundred KB's were backed up. I googled a lot and even tried microsoft tech support for details on this api.

But finally i had to write a manual application which fires an event on each change in folder and compare the last modified time vs that on the back up server and updates if it is different to support the kind of load that was required.

That always made me wonder - Was FSWatcher really practical beyond basic code samples with multi-threading support?

#3
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Below is the exact code project article sample i used and then posted my problem there. I forgot to mention that i was running this back up application as a windows service as described in the link.

Limitation of FSWatcher & Windows Service - C# Discussion Boards - CodeProject

#4
Tonchi

Tonchi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 474 posts
  • Location:Varaždin
  • Programming Language:C, C++, C#
it's not working man
fileSystemWatcher1 is nor recognized on 4 places

#5
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
The name of class is FileSystemWatcher. I thought you were using an object of your own. May this would help


FileSystemWatcher obj = new FileSystemWatcher();

obj.Path = "c:\\test\\" 

obj.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | 

                   NotifyFilters.DirectoryName | NotifyFilters.FileName;

obj.Changed += new FileSystemEventHandler(OnChanged);

obj.Created += new FileSystemEventHandler(OnCreated);

obj.Deleted += new FileSystemEventHandler(OnChanged);

obj.Renamed += new RenamedEventHandler(OnRenamed);

obj.EnableRaisingEvents = true;


A complete sample program is
A .NET File System Watcher - CodeProject

#6
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
As you can see, you need to add OnChanged , OnCreated etc events to make your functions recognized in your code.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users