Simple File System Watcher in C#
In this article I describe the usage of the FileSystemWatcher controller provided by .Net Framework , using C#
- using System;
- using System.Text;
- using System.Security.Permissions;
- using System.IO;
- namespace FileSystemWatcherC
- {
- class Program
- {
- static FileSystemWatcher watcher = null;
- [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
- static void Main(string[] args)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.BackgroundColor = ConsoleColor.Black;
- string[] argsx = System.Environment.GetCommandLineArgs();
- Console.Title = "Egle Eye";
- int len = argsx.Length;
- /*
- * Helper cordings...
- *
- * Console.WriteLine(len.ToString());
- foreach (string s in argsx)
- {
- Console.Write(s + " ");
- }
- */
- if (len < 2)
- {
- DrawHelperScreen();
- }
- else
- {
- try
- {
- watcher = new FileSystemWatcher();
- watcher.BeginInit();
- watcher.IncludeSubdirectories = true;
- SetFileTypes(string.Empty);
- SetArgs(argsx);
- }
- catch (Exception ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.BackgroundColor = ConsoleColor.Black;
- Console.WriteLine("");
- Console.WriteLine(ex.Message);
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.White;
- Console.BackgroundColor = ConsoleColor.Black;
- return;
- }
- }
- }
- private static void SetWorkinfDirecotory(string path)
- {
- watcher.Path = path;
- }
- private static void SetArgs(string[] args)
- {
- if (args.Length > 1)
- {
- SetWorkinfDirecotory(args[1]);
- }
- if (args.Length > 2)
- {
- SetWachingEvents(args[2]);
- }
- else { SetWachingEvents(""); }
- if (args.Length > 3)
- {
- SetFileTypes(args[3]);
- }
- if (args.Length > 4)
- {
- if (args[4].ToLower() == "false")
- watcher.IncludeSubdirectories = false;
- }
- /*
- * Helper cordings...
- *
- * Console.WriteLine(watcher.Path);
- Console.WriteLine(watcher.Filter);
- Console.WriteLine(watcher.IncludeSubdirectories.ToString()); */
- StartWatcher();
- }
- private static void SetWachingEvents(string filterEvents)
- {
- string[] values = filterEvents.Split(',');
- foreach (string s in values)
- {
- switch (s)
- {
- case "ch": watcher.Changed += new FileSystemEventHandler(OnChanged); break;
- case "cr": watcher.Created += new FileSystemEventHandler(OnChanged); break;
- case "de": watcher.Deleted += new FileSystemEventHandler(OnChanged); break;
- case "rn": watcher.Renamed += new RenamedEventHandler(OnRenamed); break;
- default:
- watcher.Changed += new FileSystemEventHandler(OnChanged);
- watcher.Created += new FileSystemEventHandler(OnChanged);
- watcher.Deleted += new FileSystemEventHandler(OnChanged);
- watcher.Renamed += new RenamedEventHandler(OnRenamed);
- break;
- }
- }
- }
- private static void SetFileTypes(string filetypes)
- {
- if (filetypes != String.Empty)
- watcher.Filter = filetypes;
- else watcher.Filter = "*.*";
- }
- private static void DrawHelperScreen()
- {
- //Console.Clear();
- Console.ForegroundColor = ConsoleColor.DarkCyan;
- Console.WriteLine("");
- Console.WriteLine("");
- Console.WriteLine(@"╔══════════════════════════Usage══════════════════════════╗");
- Console.WriteLine("║ ║");
- Console.WriteLine(@"║ EgleEye [Waching directory] - Ex C:\ ║");
- Console.WriteLine("║ ║");
- Console.WriteLine(@"║ Filter: ch[Change],de[Delete],cr[Created],rn[Rename] ║");
- Console.WriteLine("║ If no arguments with file path watch all events. ║");
- Console.WriteLine("║ ║");
- Console.WriteLine(@"║ EgleEye C:\ ch,de,cr,rn ║");
- Console.WriteLine(@"║ EgleEye C:\ ch,de,cr ║");
- Console.WriteLine(@"║ EgleEye C:\ ch,de ║");
- Console.WriteLine(@"║ EgleEye C:\ ch ║");
- Console.WriteLine(@"║ EgleEye C:\ ║");
- Console.WriteLine("║ ║");
- Console.WriteLine(@"║ EgleEye [File Types *.* = all , *.txt = text ] ║");
- Console.WriteLine("║ ║");
- Console.WriteLine(@"║ File Types *recipe.doc ║");
- Console.WriteLine(@"║ File Types *.txt ║");
- Console.WriteLine(@"║ File Types pass*.* ║");
- Console.WriteLine("║ ║");
- Console.WriteLine(@"║ Include Include Subdirectories for the search:off ║");
- Console.WriteLine("║ ║");
- Console.WriteLine(@"║ Subdirectories default ON ║");
- Console.WriteLine(@"║ To desable it set args: false ║");
- Console.WriteLine("║ ║");
- Console.WriteLine(@"║ Usage : ║");
- Console.WriteLine(@"║ EgleEye C:\ ch,de,cr,rn *.* false ║");
- Console.WriteLine("║ ║");
- Console.WriteLine(@"║ **NOTE : To exit press 'q' and Hit Enter ║");
- Console.WriteLine("║ ║");
- Console.WriteLine(@"╚══════════════════════════Usage══════════════════════════╝");
- return;
- }
- [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
- public static void StartWatcher()
- {
- watcher.NotifyFilter = NotifyFilters.LastAccess |
- NotifyFilters.LastWrite |
- NotifyFilters.FileName |
- NotifyFilters.DirectoryName |
- NotifyFilters.Attributes |
- NotifyFilters.Security;
- watcher.EndInit();
- watcher.EnableRaisingEvents = true;
- Console.WriteLine("Press \'q\' and hit Enter to Exit.....");
- while (Console.Read() != 'q') ;
- }
- private static void OnChanged(object source, FileSystemEventArgs e)
- {
- switch (e.ChangeType)
- {
- case WatcherChangeTypes.Changed:
- Console.BackgroundColor = ConsoleColor.Black;
- Console.ForegroundColor = ConsoleColor.DarkGreen;
- break;
- case WatcherChangeTypes.Created:
- Console.BackgroundColor = ConsoleColor.Yellow;
- Console.ForegroundColor = ConsoleColor.Black;
- break;
- case WatcherChangeTypes.Deleted:
- Console.BackgroundColor = ConsoleColor.DarkRed;
- Console.ForegroundColor = ConsoleColor.Cyan;
- break;
- }
- WriteFullLine(e.ChangeType + " : " + e.FullPath, e);
- }
- private static void OnRenamed(object source, RenamedEventArgs e)
- {
- Console.ForegroundColor = ConsoleColor.DarkCyan;
- Console.BackgroundColor = ConsoleColor.Black;
- Console.WriteLine("");
- Console.Write("Renamed :");
- Console.WriteLine(" File Old : " + e.OldFullPath);
- Console.WriteLine(" File New : " + e.FullPath);
- Console.WriteLine("");
- }
- static void WriteFullLine(string value, FileSystemEventArgs e)
- {
- Console.WriteLine(value.PadRight(Console.WindowWidth - 1));
- }
- }
- }
Comments
Post a Comment