Esempio n. 1
0
 internal RhinoFileWatcher(uint runtimeSerialNumber)
 {
     try
     {
         RuntimeSerialNumber = runtimeSerialNumber;
         Watcher             = new FileSystemWatcher
         {
             // Disable watching
             EnableRaisingEvents = false,
             // Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories.
             //ALB - I'm fairly sure we don't need to know when a file was last accessed...and it seems like this would be a bad thing
             //to notify about.
             NotifyFilter = /*NotifyFilters.LastAccess | */ NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,
         };
         // Add event handlers.
         Watcher.Changed += OnChanged;
         Watcher.Created += OnChanged;
         Watcher.Deleted += OnChanged;
         Watcher.Renamed += OnRenamed;
         // Make sure they watchers are shutdown when closing Rhino
         RhinoApp.Closing += (sender, args) => Dispose();
     }
     catch (Exception exception)
     {
         RhinoFileEventWatcherHooks.ReportException(exception);
     }
 }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
 internal RhinoFileWatcher(IntPtr pointerToIRhinoFileEventWatcher)
 {
     try
     {
         Pointer = pointerToIRhinoFileEventWatcher;
         Watcher = new FileSystemWatcher
         {
             // Disable watching
             EnableRaisingEvents = false,
             // Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories.
             NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,
         };
         // Add event handlers.
         Watcher.Changed += OnChanged;
         Watcher.Created += OnChanged;
         Watcher.Deleted += OnChanged;
         Watcher.Renamed += OnRenamed;
         // Make sure they watchers are shutdown when closing Rhino
         RhinoApp.Closing += (sender, args) => Dispose();
     }
     catch (Exception exception)
     {
         RhinoFileEventWatcherHooks.ReportException(exception);
     }
 }
Esempio n. 4
0
        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);
            }
        }
Esempio n. 5
0
        public void Dispose()
        {
            RhinoFileEventWatcherHooks.DisposedOf(RuntimeSerialNumber);

            UnWatch();

            Disposed = true;
        }
Esempio n. 6
0
        // 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);
            }
        }
Esempio n. 7
0
        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);
            }
        }