public bool Watch(string path, string filter) { try { path = ConvertPathToOSPath(path); filter = ConvertPathToOSPath(filter); // Check to see if this is a path to a file name IsFile = !Directory.Exists(path); if (IsFile) { // Use the file name as the filter filter = Path.GetFileName(path); // Get the file directory to use as the path path = Path.GetDirectoryName(path); } Watcher.EnableRaisingEvents = false; Watcher.Path = path; Watcher.Filter = filter ?? "*.*"; Watcher.EnableRaisingEvents = Enabled; return(true); } catch (Exception e) { var message = string.IsNullOrWhiteSpace(path) ? UI.Localization.LocalizeString("RhinoFileWatcher.Watch *error* empty path", 39) : string.Format(UI.Localization.LocalizeString("RhinoFileWatcher.Watch *error* parsing path name \"{0}\"", 40), path); RhinoApp.WriteLine(message); RhinoFileEventWatcherHooks.DumpException(e); return(false); } }
private void OnRenamed(object source, RenamedEventArgs args) { if (!Enabled) { return; } // 26 Feb 2021 John Morse // https://mcneel.myjetbrains.com/youtrack/issue/RH-62972 // When saving a 3DM file is finishing it calls ReplaceFileW which causes a renamed // instance which has a null e.OldName and a e.Name, this will ignore cases where // the old name is null and the new name is not or the new name is null and the old // is not. try { if (string.IsNullOrEmpty(args.Name) == string.IsNullOrEmpty(args.OldName)) { RhinoApp.InvokeOnUiThread(RenamedHook, RuntimeSerialNumber, args.OldFullPath, args.FullPath); } } catch (Exception e) { RhinoFileEventWatcherHooks.DumpException(e); } }
// Define the event handlers. private void OnChanged(object source, FileSystemEventArgs args) { if (!Enabled) { return; } try { RhinoApp.InvokeOnUiThread(ChangedHook, RuntimeSerialNumber, (RhinoFileWatcherChangeReason)args.ChangeType, args.FullPath); } catch (Exception e) { RhinoFileEventWatcherHooks.DumpException(e); } }
public bool Watch(string path, string filter) { if (Disposed) { return(false); } UnWatch(); try { UnWatch(); path = ConvertPathToOSPath(path); filter = ConvertPathToOSPath(filter); // Check to see if this is a path to a file name IsFile = !Directory.Exists(path); if (IsFile) { // Use the file name as the filter filter = Path.GetFileName(path); // Get the file directory to use as the path path = Path.GetDirectoryName(path); } //Now find a corresponding watcher from the dictionary string key = System.IO.Path.Combine(path, filter); RefCountedFileSystemWatcher watcher = null; if (g_file_system_watchers.TryGetValue(key, out watcher) && null != watcher) { Watcher = watcher; Watcher.AddRef(); } else { watcher = new RefCountedFileSystemWatcher(); g_file_system_watchers.Add(key, watcher); watcher.Impl.Path = path; watcher.Impl.Filter = filter ?? "*.*"; Watcher = watcher; } Watcher.Impl.Changed += OnChanged; Watcher.Impl.Created += OnChanged; Watcher.Impl.Deleted += OnChanged; Watcher.Impl.Renamed += OnRenamed; Watcher.Impl.EnableRaisingEvents = true; return(true); } catch (Exception e) { var message = string.IsNullOrWhiteSpace(path) ? UI.Localization.LocalizeString("RhinoFileWatcher.Watch *error* empty path", 39) : string.Format(UI.Localization.LocalizeString("RhinoFileWatcher.Watch *error* parsing path name \"{0}\"", 40), path); RhinoApp.WriteLine(message); RhinoFileEventWatcherHooks.DumpException(e); return(false); } }