Monitoring file creation using FileSystemWatcher class

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Diagnostics;


namespace Test


    class Watcher


        static void Main(string[] args)





        public static void Run()


            // Create a new FileSystemWatcher and set its properties.

            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.Path = @"D:\Test\";


            // Add event handlers.

            watcher.Created += new FileSystemEventHandler(OnCreated);


            // Begin watching.

            watcher.EnableRaisingEvents = true;


            // Wait for the user to quit the program.

            Console.WriteLine("Press \’q\’ to quit the sample.");

            while (Console.Read() != ‘q’) ;



        // Define the event handlers.

        private static void OnCreated(object source, FileSystemEventArgs e)


            // Specify what is done when a file is changed, created, or deleted.

            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);





This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s