private void StartWatcher(ShellObject shellObject) { if (_watcher != null) { _watcher.Dispose(); } eventStack.Children.Clear(); txtPath.Text = shellObject.ParsingName; _watcher = new ShellObjectWatcher(shellObject, chkRecursive.IsChecked ?? true); _watcher.AllEvents += AllEventsHandler; _watcher.Start(); }
private static void TestWatcher() { ShellObject so = ShellObject.FromParsingName("C:"); ShellObjectWatcher sw = new ShellObjectWatcher(so, true); sw.ItemCreated += new EventHandler<ShellObjectChangedEventArgs>(watcher_ItemCreated); sw.ItemDeleted += new EventHandler<ShellObjectChangedEventArgs>(sw_ItemDeleted); sw.ItemRenamed += new EventHandler<ShellObjectRenamedEventArgs>(sw_ItemRenamed); sw.DirectoryCreated += new EventHandler<ShellObjectChangedEventArgs>(sw_DirectoryCreated); sw.DirectoryDeleted += new EventHandler<ShellObjectChangedEventArgs>(sw_DirectoryDeleted); sw.DirectoryRenamed += new EventHandler<ShellObjectRenamedEventArgs>(sw_DirectoryRenamed); sw.DirectoryUpdated += new EventHandler<ShellObjectChangedEventArgs>(sw_DirectoryUpdated); sw.Start(); Console.ReadLine(); sw.Stop(); Console.WriteLine("End"); }
private void TestMethod(string eventName, Action<string> test, string path) { var shellObject = ShellObject.FromParsingName(path); using (AutoResetEvent evt = new AutoResetEvent(false)) using (ShellObjectWatcher watcher = new ShellObjectWatcher(shellObject, true)) { bool success = false; Action successEvent = new Action(() => { success = true; evt.Set(); }); var changedHandler = new EventHandler<ShellObjectChangedEventArgs>((sender, args) => successEvent()); var renamedHandler = new EventHandler<ShellObjectRenamedEventArgs>((sender, args) => successEvent()); //register for event var addMethod = typeof(ShellObjectWatcher) .GetEvents() .FirstOrDefault(x => x.Name.Equals(eventName, StringComparison.InvariantCultureIgnoreCase)) .GetAddMethod(); var paramType = addMethod.GetParameters()[0].ParameterType .GetGenericArguments()[0]; if (paramType == typeof(ShellObjectChangedEventArgs)) { addMethod.Invoke(watcher, new object[] { changedHandler }); } else if (paramType == typeof(ShellObjectRenamedEventArgs)) { addMethod.Invoke(watcher, new object[] { renamedHandler }); } else { throw new Exception("Unknown handler type."); } //start watcher.Start(); test(path); evt.WaitOne(TimeoutMS); Assert.True(success); } }