public FormMods() { InitializeComponent(); CenterToScreen(); listBoxInstalled.DataSource = new BindingList <ModListing>(); listBoxNotInstalled.DataSource = new BindingList <ModListing>(); UpdateModList(); var feh = new System.IO.FileSystemEventHandler(OnFileEvent); var feh2 = new System.IO.RenamedEventHandler(OnFileEvent); System.IO.FileSystemWatcher watcher1 = new System.IO.FileSystemWatcher { Path = MainForm.Settings.NotInstalledModsDirectory, NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName, Filter = "*.botm.zip" }; watcher1.Created += feh; watcher1.Changed += feh; watcher1.Renamed += feh2; watcher1.Deleted += feh; watcher1.EnableRaisingEvents = true; System.IO.FileSystemWatcher watcher2 = new System.IO.FileSystemWatcher { Path = MainForm.Settings.NotInstalledModsDirectory, NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName, Filter = "*.botm.zip" }; watcher2.Created += feh; watcher2.Deleted += feh; watcher2.EnableRaisingEvents = true; }
private static System.IO.FileSystemWatcher CreateWatcher(string dir, BlockingCollection <string> files) { var watcher = new System.IO.FileSystemWatcher(dir, "*.cs") { IncludeSubdirectories = true, InternalBufferSize = 1000000 }; System.IO.FileSystemEventHandler handler = (_s, _e) => { Console.WriteLine(_e.FullPath); Console.WriteLine(" {0}, {1}", _e.ChangeType, _e.Name); if (!_e.Name.EndsWith(".cs", StringComparison.InvariantCultureIgnoreCase)) { return; } files.TryAdd(_e.FullPath); }; watcher.Changed += handler; watcher.Created += handler; watcher.Deleted += handler; watcher.Renamed += (_s, _e) => handler(_s, _e); watcher.NotifyFilter = System.IO.NotifyFilters.FileName; watcher.EnableRaisingEvents = true; return(watcher); }
//Automatically raise event in calling thread when _fsw.SynchronizingObject is set. Ex: When used as a component in Win Forms. //TODO: remove redundancy. I don't understand how to cast the specific *EventHandler to a generic Delegate, EventHandler, Action or whatever. private void InvokeHandler(FileSystemEventHandler eventHandler, FileSystemEventArgs e) { if (eventHandler != null) { if (_containedFSW.SynchronizingObject != null && this._containedFSW.SynchronizingObject.InvokeRequired) { _containedFSW.SynchronizingObject.BeginInvoke(eventHandler, new object[] { this, e }); } else { eventHandler(this, e); } } }