Пример #1
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);
            }
        }