Advertisement
Fhernd

Monitor.cs

Jul 20th, 2015
1,472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. // OrtizOL - xCSw - http://ortizol.blogspot.com
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Security.Permissions;
  6.  
  7. namespace Receta.CSharp.R0519
  8. {
  9.     public class Monitor
  10.     {
  11.         public static void Main()
  12.         {
  13.             Console.WriteLine(Environment.NewLine);
  14.            
  15.             EjecutarMonitor();
  16.            
  17.             Console.WriteLine(Environment.NewLine);
  18.         }
  19.        
  20.         [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
  21.         public static void EjecutarMonitor()
  22.         {
  23.             string[] args = System.Environment.GetCommandLineArgs();
  24.            
  25.             // Valida el número de argumentos introducidos
  26.             // por el usuario:
  27.             if (args.Length != 2)
  28.             {
  29.                 Console.WriteLine("Uso: Monitor.exe (directorio)");
  30.                 return;
  31.             }
  32.            
  33.             // Creación de objeto FileSystemWatcher:
  34.             FileSystemWatcher monitor = new FileSystemWatcher();
  35.            
  36.             // Directorio a monitorizar:
  37.             monitor.Path = args[1];
  38.            
  39.             // Cambios a monitorizar:
  40.             monitor.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
  41.                                   | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  42.            
  43.             // Tipos de archivos a monitorizar:
  44.             monitor.Filter = "*.txt";
  45.            
  46.             // Adición de manejadores de eventos:
  47.             monitor.Changed += new FileSystemEventHandler(OnChanged);
  48.             monitor.Created += new FileSystemEventHandler(OnChanged);
  49.             monitor.Deleted += new FileSystemEventHandler(OnChanged);
  50.             monitor.Renamed += new RenamedEventHandler(OnRenamed);
  51.            
  52.             // Inicio de la monitorización:
  53.             monitor.EnableRaisingEvents = true;
  54.            
  55.             Console.WriteLine(args[1]);
  56.            
  57.             // El usuario finaliza el programa con presionar
  58.             // la tecla q:
  59.             Console.WriteLine("Presione \'q\' para finalizar la monitorización.");
  60.             while (Console.Read() != 'q');
  61.         }
  62.        
  63.         // Manejador de cambios sobre el archivo: Changed, Created, y Deleted:
  64.         private static void OnChanged(object source, FileSystemEventArgs e)
  65.         {
  66.             Console.WriteLine ("El archivo {0} fue {1}.", e.FullPath, e.ChangeType);
  67.         }
  68.        
  69.         // Manejador evento de renombrado: Renamed
  70.         private static void OnRenamed(object source, RenamedEventArgs e)
  71.         {
  72.             Console.WriteLine("El archivo {0} fue renombrado a {1}", e.OldFullPath, e.FullPath);
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement