Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // OrtizOL - xCSw - http://ortizol.blogspot.com
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Windows.Forms;
  6.  
  7. namespace Receta.CSharp.R0519
  8. {
  9.     public class MonitorCreacionEliminacionArchivo
  10.     {
  11.         public static void Main()
  12.         {
  13.             Console.WriteLine(Environment.NewLine);
  14.            
  15.             // Creación del monitor de sistema de archivo:
  16.             using (FileSystemWatcher monitor = new FileSystemWatcher())
  17.             {
  18.                 // Ruta a monitorizar:
  19.                 monitor.Path = Application.StartupPath;
  20.                
  21.                 // Tipos de archivos a monitorizar:
  22.                 monitor.Filter = "*.*";
  23.                
  24.                 // Incluir subdirectorios:
  25.                 monitor.IncludeSubdirectories = true;
  26.                
  27.                 // Adición de manejadores (handlers) de evento:
  28.                 monitor.Created += new FileSystemEventHandler(OnCreatedODeleted);
  29.                 monitor.Deleted += new FileSystemEventHandler(OnCreatedODeleted);
  30.                
  31.                 // Inicio de la monitorización:
  32.                 monitor.EnableRaisingEvents = true;
  33.                
  34.                 Console.WriteLine("Presione Enter para crear un archivo: ");
  35.                 Console.ReadLine ();
  36.                
  37.                 using (FileStream fs = new FileStream("Archivo.bin", FileMode.Create))
  38.                 {
  39.                     // Escritura de datos...
  40.                 }
  41.                
  42.                 Console.WriteLine("\nPresione Enter para eliminar un archivo: ");
  43.                 Console.ReadLine ();
  44.                
  45.                 if (File.Exists("Archivo.bin"))
  46.                 {
  47.                     File.Delete("Archivo.bin");
  48.                 }
  49.             }
  50.            
  51.             Console.WriteLine(Environment.NewLine);
  52.         }
  53.        
  54.         private static void OnCreatedODeleted(object sender, FileSystemEventArgs e)
  55.         {
  56.             Console.WriteLine("\tNotificación: {0} fue -{1}-.", e.FullPath, e.ChangeType.ToString());
  57.         }
  58.     }
  59. }