Watch for filesystem renamed and changed event in CSharp

Description

The following code shows how to watch for filesystem renamed and changed event.

Example


  /*  w  w w. j  a  va  2s .  c o  m*/

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Net.Mail;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;

public class MainClass
{
    public static void Main()
    {
        FileSystemWatcher watcher = new FileSystemWatcher(@"c:\", "*.*");
        // only rename event
        WaitForChangedResult result =watcher.WaitForChanged(WatcherChangeTypes.Changed | WatcherChangeTypes.Renamed);

        switch (result.ChangeType)
        {
            case WatcherChangeTypes.Changed:
                Console.WriteLine("{0}: File '{1}' was changed",DateTime.Now, result.Name);
                break;
            case WatcherChangeTypes.Renamed:
                Console.WriteLine("{0}: File '{1}' was renamed to '{2}'",DateTime.Now, result.OldName, result.Name);
                break;
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip