Created
October 6, 2020 14:54
-
-
Save jocelyn/05512d1e83585b6bd977a8ce7dd1d753 to your computer and use it in GitHub Desktop.
watcher.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.Security.Permissions; | |
| using System.Diagnostics; | |
| using System.Timers; | |
| using System.Collections.Generic; | |
| public class Watcher | |
| { | |
| private static System.Timers.Timer aTimer; | |
| public static int timer_delay=5000; | |
| public static string command; | |
| public static bool in_action; | |
| public static void Main() | |
| { | |
| Run(); | |
| } | |
| [PermissionSet(SecurityAction.Demand, Name = "FullTrust")] | |
| private static void Run() | |
| { | |
| string[] args = Environment.GetCommandLineArgs(); | |
| List<string> dirs = new List<string>(); | |
| string delay=null; | |
| string filter=null; | |
| int resp; | |
| FileSystemWatcher watcher; | |
| // If a directory is not specified, exit program. | |
| for (int i = 1; i < args.Length; i++) { | |
| if (args[i].Equals("--command")) { | |
| command = args[i + 1]; | |
| i = i + 1; | |
| } else if (args[i].Equals("--delay")) { | |
| delay = args[i + 1]; | |
| i = i + 1; | |
| } else if (args[i].Equals("--filter")) { | |
| filter = args[i + 1]; | |
| i = i + 1; | |
| } else { | |
| dirs.Add(args[i]); | |
| } | |
| } | |
| if (command != null) { | |
| Console.WriteLine("Command: " + command); | |
| } | |
| if (filter != null) { | |
| Console.WriteLine("Filter: " + filter); | |
| } | |
| if (delay != null) { | |
| Console.WriteLine("Use delay: "+ delay + " ms."); | |
| timer_delay = int.Parse(delay); | |
| } | |
| Console.WriteLine("Directories:"); | |
| foreach (string d in dirs) { | |
| Console.WriteLine(" - " + d); | |
| } | |
| if (dirs.Count == 0 | command == null) { | |
| Console.WriteLine("Usage: Watcher.exe --command action --delay 10ms --filter \"*.*\" {directories}"); | |
| return; | |
| } | |
| foreach (string d in dirs) { | |
| watcher = new FileSystemWatcher(); | |
| watcher.Path = d; | |
| watcher.IncludeSubdirectories = true; | |
| // Watch for changes in LastAccess and LastWrite times, and | |
| // the renaming of files or directories. | |
| watcher.NotifyFilter = NotifyFilters.LastAccess | |
| | NotifyFilters.LastWrite | |
| | NotifyFilters.FileName | |
| | NotifyFilters.DirectoryName; | |
| if (filter != null) { | |
| watcher.Filter = filter; //"*.css"; | |
| } | |
| // Add event handlers. | |
| watcher.Changed += OnChanged; | |
| watcher.Created += OnCreated; | |
| watcher.Deleted += OnDeleted; | |
| watcher.Renamed += OnRenamed; | |
| // Begin watching. | |
| watcher.EnableRaisingEvents = true; | |
| } | |
| // Wait for the user to quit the program. | |
| Console.WriteLine("Press 'q' to quit or 'f' to force execution."); | |
| resp = ' '; | |
| while (resp != 'q') { | |
| resp = Console.Read(); | |
| if (resp == 'f') { | |
| trigger_execution(); | |
| Console.WriteLine("ok."); | |
| } | |
| }; | |
| } | |
| private static void launch_install(string p) | |
| { | |
| if (!in_action) { | |
| if (aTimer != null) { | |
| aTimer.Stop(); | |
| aTimer.Start(); | |
| } else { | |
| SetTimer(); | |
| } | |
| } | |
| } | |
| private static void SetTimer() | |
| { | |
| // Create a timer with a two second interval. | |
| aTimer = new System.Timers.Timer(5000); | |
| // Hook up the Elapsed event for the timer. | |
| aTimer.Elapsed += OnTimedEvent; | |
| aTimer.AutoReset = true; | |
| aTimer.Enabled = true; | |
| } | |
| private static void OnTimedEvent(Object source, ElapsedEventArgs e) | |
| { | |
| in_action = true; | |
| //Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}", e.SignalTime); | |
| aTimer.Stop(); | |
| aTimer = null; | |
| if (command != null) { | |
| trigger_execution(); | |
| } else { | |
| Console.WriteLine("File changed"); | |
| } | |
| in_action = false; | |
| } | |
| private static void trigger_execution() | |
| { | |
| if (command != null) { | |
| Console.WriteLine("Launch command: " + command); | |
| try | |
| { | |
| var process = new Process | |
| { | |
| StartInfo = new ProcessStartInfo{ | |
| FileName = command, | |
| UseShellExecute = false, | |
| RedirectStandardOutput = true, | |
| CreateNoWindow = true | |
| } | |
| }; | |
| //"e:\\eiffelstudio\\trunk\\Src\\web\\eiffel-cloud\\install.bat"); | |
| process.Start(); | |
| while (!process.StandardOutput.EndOfStream) | |
| { | |
| var line = process.StandardOutput.ReadLine(); | |
| Console.WriteLine(line); | |
| } | |
| process.WaitForExit(); | |
| } | |
| catch (Exception e) | |
| { | |
| Console.WriteLine(e.Message); | |
| } | |
| } | |
| } | |
| // Define the event handlers. | |
| private static void OnNotified(object source, FileSystemEventArgs e) => launch_install($"{e.FullPath}"); | |
| private static void OnChanged(object source, FileSystemEventArgs e) | |
| { | |
| // Specify what is done when a file is changed, created, or deleted. | |
| Console.WriteLine($"File changed: {e.FullPath} {e.ChangeType}"); | |
| OnNotified(source, e); | |
| } | |
| private static void OnRenamed(object source, RenamedEventArgs e) | |
| // Specify what is done when a file is renamed. | |
| { | |
| Console.WriteLine($"File: {e.OldFullPath} renamed to {e.FullPath}"); | |
| OnNotified(source, e); | |
| } | |
| private static void OnCreated(object source, FileSystemEventArgs e) | |
| // Specify what is done when a file is renamed. | |
| { | |
| Console.WriteLine($"File created: {e.FullPath}"); | |
| OnNotified(source, e); | |
| } | |
| private static void OnDeleted(object source, FileSystemEventArgs e) | |
| // Specify what is done when a file is renamed. | |
| { | |
| Console.WriteLine($"File deleted: {e.FullPath}"); | |
| OnNotified(source, e); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment