File System watcher

In this chapter you will learn:

  1. How to monitor file system

Monitor file system

using System;/*from   java2  s  .  c  o  m*/
using System.IO;

public class Main 
{

  // event handler for file change
  public static void OnChanged(object source, FileSystemEventArgs e) 
  {
    // dump info to the screen
    Console.WriteLine("Change to " + e.FullPath + ": " +
     e.ChangeType);
  }

  public static void Main() 
  {

    // create a watcher for the c: drive
    FileSystemWatcher fsw = new FileSystemWatcher("c:\\");
    fsw.IncludeSubdirectories = true;

    // hook up the event handler
    fsw.Changed += new FileSystemEventHandler(OnChanged);

    // turn on file watching
    fsw.EnableRaisingEvents = true;
    
    // And wait for the user to quit
    Console.WriteLine("Press any key to exit");
    int i = Console.Read();

  }

}

Next chapter...

What you will learn in the next chapter:

  1. Key Members of the System.IO Namespace
Home » C# Tutorial » File, Path
File creation
Deleting Files
Put to recycle bin
File Exists
File moving
File size
File attribute
Set file attribute
File time change
File time
Random file name
Temporary file
Path
Directory creation
Delete directory
Path existance
Change the path
Current Path
Directory listing
Recursive Path Listing
Combine path
Calculate Directory Size
Invalid path characters
System path
Directory copy
Drive
Drive information
File access rule
File System watcher