On file created or deleted : FileSystemWatcher « File Stream « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » File Stream » FileSystemWatcherScreenshots 
On file created or deleted
 


using System;
using System.IO;
using System.Windows.Forms;

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

            // Attach the event handler.
            watch.Created += new FileSystemEventHandler(OnCreatedOrDeleted);
            watch.Deleted += new FileSystemEventHandler(OnCreatedOrDeleted);
            watch.EnableRaisingEvents = true;

            Console.WriteLine("Press Enter to create a  file.");
            Console.ReadLine();

            if (File.Exists("test.bin")) {
                File.Delete("test.bin");
            }

            // Create test.bin.
            using (FileStream fs = new FileStream("test.bin", FileMode.Create)) {
                // Do something.
            }

            Console.WriteLine("Press Enter to terminate the application.");
            Console.ReadLine();
        }
    }

    private static void OnCreatedOrDeleted(object sender, FileSystemEventArgs e) {
        Console.WriteLine("\tNOTIFICATION: " + e.FullPath + "' was " + e.ChangeType.ToString());
        Console.WriteLine();
    }
}
           
         
  
Related examples in the same category
1.Set NotifyFilter of FileSystemWatcher
2.Listens to the file system change notifications and raises events
ww___w___._j___av___a___2_s__.__com__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.