Monitor the File System for Changes - CSharp File IO

CSharp examples for File IO:File Watcher

Description

Monitor the File System for Changes

Demo Code


using System;/* w w w.j  a  v a2s. com*/
using System.IO;
using System.Windows.Forms;

    static class MainClass
    {
        static void Main()
        {
            using (FileSystemWatcher watch = new FileSystemWatcher())
            {
                watch.Path = Application.StartupPath;
                watch.Filter = "*.*";
                watch.IncludeSubdirectories = true;

                watch.Created += new FileSystemEventHandler(OnCreatedOrDeleted);
                watch.Deleted += new FileSystemEventHandler(OnCreatedOrDeleted);
                watch.EnableRaisingEvents = true;

                if (File.Exists("test.bin"))
                {
                    File.Delete("test.bin");
                }
                using (FileStream fs = new FileStream("test.bin", FileMode.Create))
                {
                    // Do something.
                }
            }
        }
        private static void OnCreatedOrDeleted(object sender,FileSystemEventArgs e)
        {
            Console.WriteLine("\tNOTIFICATION: " + e.FullPath + "' was " + e.ChangeType.ToString());
        }
    }

Related Tutorials