2 // System.IO.FileSystemWatcher.cs
5 // Tim Coleman (tim@timcoleman.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // Copyright (C) Tim Coleman, 2002
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.ComponentModel;
35 using System.Diagnostics;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.InteropServices;
38 using System.Security.Permissions;
39 using System.Threading;
42 [DefaultEvent("Changed")]
46 public class FileSystemWatcher : Component, ISupportInitialize {
50 bool enableRaisingEvents;
52 bool includeSubdirectories;
53 int internalBufferSize;
54 NotifyFilters notifyFilter;
57 ISynchronizeInvoke synchronizingObject;
58 WaitForChangedResult lastData;
60 SearchPattern2 pattern;
63 static IFileWatcher watcher;
64 static object lockobj = new object ();
70 public FileSystemWatcher ()
72 this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
73 this.enableRaisingEvents = false;
75 this.includeSubdirectories = false;
76 this.internalBufferSize = 8192;
81 public FileSystemWatcher (string path)
86 public FileSystemWatcher (string path, string filter)
89 throw new ArgumentNullException ("path");
92 throw new ArgumentNullException ("filter");
94 if (path == String.Empty)
95 throw new ArgumentException ("Empty path", "path");
97 if (!Directory.Exists (path))
98 throw new ArgumentException ("Directory does not exists", "path");
100 this.enableRaisingEvents = false;
101 this.filter = filter;
102 this.includeSubdirectories = false;
103 this.internalBufferSize = 8192;
104 this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
106 this.synchronizingObject = null;
110 [EnvironmentPermission (SecurityAction.Assert, Read="MONO_MANAGED_WATCHER")]
117 string managed = Environment.GetEnvironmentVariable ("MONO_MANAGED_WATCHER");
120 mode = InternalSupportsFSW ();
125 ok = DefaultWatcher.GetInstance (out watcher);
126 //ok = WindowsWatcher.GetInstance (out watcher);
129 ok = FAMWatcher.GetInstance (out watcher, false);
132 ok = KeventWatcher.GetInstance (out watcher);
135 ok = FAMWatcher.GetInstance (out watcher, true);
138 ok = InotifyWatcher.GetInstance (out watcher, true);
142 if (mode == 0 || !ok) {
143 if (String.Compare (managed, "disabled", true) == 0)
144 NullFileWatcher.GetInstance (out watcher);
146 DefaultWatcher.GetInstance (out watcher);
153 [Conditional ("DEBUG"), Conditional ("TRACE")]
154 void ShowWatcherInfo ()
156 Console.WriteLine ("Watcher implementation: {0}", watcher != null ? watcher.GetType ().ToString () : "<none>");
159 #endregion // Constructors
163 /* If this is enabled, we Pulse this instance */
164 internal bool Waiting {
165 get { return waiting; }
166 set { waiting = value; }
169 internal string MangledFilter {
174 if (mangledFilter != null)
175 return mangledFilter;
177 string filterLocal = "*.*";
178 if (!(watcher.GetType () == typeof (WindowsWatcher)))
185 internal SearchPattern2 Pattern {
187 if (pattern == null) {
188 pattern = new SearchPattern2 (MangledFilter);
194 internal string FullPath {
196 if (fullpath == null) {
197 if (path == null || path == "")
198 fullpath = Environment.CurrentDirectory;
200 fullpath = System.IO.Path.GetFullPath (path);
207 [DefaultValue(false)]
208 [IODescription("Flag to indicate if this instance is active")]
209 public bool EnableRaisingEvents {
210 get { return enableRaisingEvents; }
212 if (value == enableRaisingEvents)
213 return; // Do nothing
215 enableRaisingEvents = value;
224 [DefaultValue("*.*")]
225 [IODescription("File name filter pattern")]
226 [RecommendedAsConfigurable(true)]
227 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
228 public string Filter {
229 get { return filter; }
231 if (value == null || value == "")
234 if (filter != value) {
237 mangledFilter = null;
242 [DefaultValue(false)]
243 [IODescription("Flag to indicate we want to watch subdirectories")]
244 public bool IncludeSubdirectories {
245 get { return includeSubdirectories; }
247 if (includeSubdirectories == value)
250 includeSubdirectories = value;
251 if (value && enableRaisingEvents) {
260 public int InternalBufferSize {
261 get { return internalBufferSize; }
263 if (internalBufferSize == value)
269 internalBufferSize = value;
270 if (enableRaisingEvents) {
277 [DefaultValue(NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite)]
278 [IODescription("Flag to indicate which change event we want to monitor")]
279 public NotifyFilters NotifyFilter {
280 get { return notifyFilter; }
282 if (notifyFilter == value)
285 notifyFilter = value;
286 if (enableRaisingEvents) {
294 [IODescription("The directory to monitor")]
295 [RecommendedAsConfigurable(true)]
296 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
297 [Editor ("System.Diagnostics.Design.FSWPathEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
305 Exception exc = null;
308 exists = Directory.Exists (value);
309 } catch (Exception e) {
314 throw new ArgumentException ("Invalid directory name", "value", exc);
317 throw new ArgumentException ("Directory does not exists", "value");
321 if (enableRaisingEvents) {
329 public override ISite Site {
330 get { return base.Site; }
331 set { base.Site = value; }
335 [IODescription("The object used to marshal the event handler calls resulting from a directory change")]
339 public ISynchronizeInvoke SynchronizingObject {
340 get { return synchronizingObject; }
341 set { synchronizingObject = value; }
344 #endregion // Properties
348 public void BeginInit ()
350 // Not necessary in Mono
353 protected override void Dispose (bool disposing)
360 base.Dispose (disposing);
363 ~FileSystemWatcher ()
369 public void EndInit ()
371 // Not necessary in Mono
379 private void RaiseEvent (Delegate ev, EventArgs arg, EventType evtype)
384 if (synchronizingObject == null) {
386 case EventType.RenameEvent:
387 ((RenamedEventHandler)ev).BeginInvoke (this, (RenamedEventArgs) arg, null, null);
389 case EventType.ErrorEvent:
390 ((ErrorEventHandler)ev).BeginInvoke (this, (ErrorEventArgs) arg, null, null);
392 case EventType.FileSystemEvent:
393 ((FileSystemEventHandler)ev).BeginInvoke (this, (FileSystemEventArgs) arg, null, null);
399 synchronizingObject.BeginInvoke (ev, new object [] {this, arg});
402 protected void OnChanged (FileSystemEventArgs e)
404 RaiseEvent (Changed, e, EventType.FileSystemEvent);
407 protected void OnCreated (FileSystemEventArgs e)
409 RaiseEvent (Created, e, EventType.FileSystemEvent);
412 protected void OnDeleted (FileSystemEventArgs e)
414 RaiseEvent (Deleted, e, EventType.FileSystemEvent);
417 protected void OnError (ErrorEventArgs e)
419 RaiseEvent (Error, e, EventType.ErrorEvent);
422 protected void OnRenamed (RenamedEventArgs e)
424 RaiseEvent (Renamed, e, EventType.RenameEvent);
427 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType)
429 return WaitForChanged (changeType, Timeout.Infinite);
432 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout)
434 WaitForChangedResult result = new WaitForChangedResult ();
435 bool prevEnabled = EnableRaisingEvents;
437 EnableRaisingEvents = true;
442 gotData = Monitor.Wait (this, timeout);
444 result = this.lastData;
447 EnableRaisingEvents = prevEnabled;
449 result.TimedOut = true;
454 internal void DispatchEvents (FileAction act, string filename, ref RenamedEventArgs renamed)
457 lastData = new WaitForChangedResult ();
461 case FileAction.Added:
462 lastData.Name = filename;
463 lastData.ChangeType = WatcherChangeTypes.Created;
464 OnCreated (new FileSystemEventArgs (WatcherChangeTypes.Created, path, filename));
466 case FileAction.Removed:
467 lastData.Name = filename;
468 lastData.ChangeType = WatcherChangeTypes.Deleted;
469 OnDeleted (new FileSystemEventArgs (WatcherChangeTypes.Deleted, path, filename));
471 case FileAction.Modified:
472 lastData.Name = filename;
473 lastData.ChangeType = WatcherChangeTypes.Changed;
474 OnChanged (new FileSystemEventArgs (WatcherChangeTypes.Changed, path, filename));
476 case FileAction.RenamedOldName:
477 if (renamed != null) {
480 lastData.OldName = filename;
481 lastData.ChangeType = WatcherChangeTypes.Renamed;
482 renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, filename, "");
484 case FileAction.RenamedNewName:
485 lastData.Name = filename;
486 lastData.ChangeType = WatcherChangeTypes.Renamed;
487 if (renamed == null) {
488 renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, "", filename);
500 watcher.StartDispatching (this);
505 watcher.StopDispatching (this);
507 #endregion // Methods
509 #region Events and Delegates
511 [IODescription("Occurs when a file/directory change matches the filter")]
512 public event FileSystemEventHandler Changed;
514 [IODescription("Occurs when a file/directory creation matches the filter")]
515 public event FileSystemEventHandler Created;
517 [IODescription("Occurs when a file/directory deletion matches the filter")]
518 public event FileSystemEventHandler Deleted;
521 public event ErrorEventHandler Error;
523 [IODescription("Occurs when a file/directory rename matches the filter")]
524 public event RenamedEventHandler Renamed;
526 #endregion // Events and Delegates
528 /* 0 -> not supported */
534 [MethodImplAttribute(MethodImplOptions.InternalCall)]
535 static extern int InternalSupportsFSW ();