FileSystemWatcher complete Example using

Using this example we can create a file watch for C directory. If any modification are happening under that directory we will get the notification like file modification, file deletion, file creation and so on.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FileWatcherConsole
    class Program
        static void Main(string[] args)
             args = System.Environment.GetCommandLineArgs();
             args[0] = "C:\\";
            // If a directory is not specified, exit program.
            if (args.Length != 1)
                // Display the proper way to call the program.
                Console.WriteLine("Usage: FileWatcher.exe <directory>");
                // Create a new FileSystemWatcher and set its properties.
                FileSystemWatcher watcher = new FileSystemWatcher();
                watcher.Path = args[0];

                // Watch both files and subdirectories.
                watcher.IncludeSubdirectories = true;

                // Watch for all changes specified in the NotifyFilters
                watcher.NotifyFilter = NotifyFilters.Attributes |
                NotifyFilters.CreationTime |
                NotifyFilters.DirectoryName |
                NotifyFilters.FileName |
                NotifyFilters.LastAccess |
                NotifyFilters.LastWrite |
                NotifyFilters.Security |

                // Watch all files.
                watcher.Filter = "*.*";

                // Add event handlers.
                watcher.Changed += new FileSystemEventHandler(OnChanged);
                watcher.Created += new FileSystemEventHandler(OnChanged);
                watcher.Deleted += new FileSystemEventHandler(OnChanged);
                watcher.Renamed += new RenamedEventHandler(OnRenamed);

                //Start monitoring.
                watcher.EnableRaisingEvents = true;

                //Do some changes now to the directory.
                //Create a DirectoryInfo object.
                DirectoryInfo d1 = new DirectoryInfo(args[0]);

                // Wait for user to quit program.
                Console.WriteLine("Press \'q\' to quit the sample.");

                //Make an infinite loop till 'q' is pressed.
                while (Console.Read() != 'q') ;

            catch (IOException e)
                Console.WriteLine("A Exception Occurred :" + e);

            catch (Exception oe)
                Console.WriteLine("An Exception Occurred :" + oe);
        // Define the event handlers.
        public static void OnChanged(object source, FileSystemEventArgs e)
            // Specify what is done when a file is changed.
            Console.WriteLine("{0}, with path {1} has been {2}", e.Name, e.FullPath, e.ChangeType);

        public static void OnRenamed(object source, RenamedEventArgs e)
            // Specify what is done when a file is renamed.
            Console.WriteLine(" {0} renamed to {1}", e.OldFullPath, e.FullPath);


If you have any queries or suggestions, please feel free to ask in comments section.
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar