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")]
44 public class FileSystemWatcher : Component, ISupportInitialize {
48 bool enableRaisingEvents;
50 bool includeSubdirectories;
51 int internalBufferSize;
52 NotifyFilters notifyFilter;
55 ISynchronizeInvoke synchronizingObject;
56 WaitForChangedResult lastData;
58 SearchPattern2 pattern;
61 static IFileWatcher watcher;
62 static object lockobj = new object ();
68 public FileSystemWatcher ()
70 this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
71 this.enableRaisingEvents = false;
73 this.includeSubdirectories = false;
74 this.internalBufferSize = 8192;
79 public FileSystemWatcher (string path)
84 public FileSystemWatcher (string path, string filter)
87 throw new ArgumentNullException ("path");
90 throw new ArgumentNullException ("filter");
92 if (path == String.Empty)
93 throw new ArgumentException ("Empty path", "path");
95 if (!Directory.Exists (path))
96 throw new ArgumentException ("Directory does not exist", "path");
98 this.enableRaisingEvents = false;
100 this.includeSubdirectories = false;
101 this.internalBufferSize = 8192;
102 this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
104 this.synchronizingObject = null;
108 [EnvironmentPermission (SecurityAction.Assert, Read="MONO_MANAGED_WATCHER")]
115 string managed = Environment.GetEnvironmentVariable ("MONO_MANAGED_WATCHER");
118 mode = InternalSupportsFSW ();
123 ok = DefaultWatcher.GetInstance (out watcher);
124 //ok = WindowsWatcher.GetInstance (out watcher);
127 ok = FAMWatcher.GetInstance (out watcher, false);
130 ok = KeventWatcher.GetInstance (out watcher);
133 ok = FAMWatcher.GetInstance (out watcher, true);
136 ok = InotifyWatcher.GetInstance (out watcher, true);
140 if (mode == 0 || !ok) {
141 if (String.Compare (managed, "disabled", true) == 0)
142 NullFileWatcher.GetInstance (out watcher);
144 DefaultWatcher.GetInstance (out watcher);
151 [Conditional ("DEBUG"), Conditional ("TRACE")]
152 void ShowWatcherInfo ()
154 Console.WriteLine ("Watcher implementation: {0}", watcher != null ? watcher.GetType ().ToString () : "<none>");
157 #endregion // Constructors
161 /* If this is enabled, we Pulse this instance */
162 internal bool Waiting {
163 get { return waiting; }
164 set { waiting = value; }
167 internal string MangledFilter {
172 if (mangledFilter != null)
173 return mangledFilter;
175 string filterLocal = "*.*";
176 if (!(watcher.GetType () == typeof (WindowsWatcher)))
183 internal SearchPattern2 Pattern {
185 if (pattern == null) {
186 if (watcher.GetType () == typeof (KeventWatcher))
187 pattern = new SearchPattern2 (MangledFilter, true); //assume we want to ignore case (OS X)
189 pattern = new SearchPattern2 (MangledFilter);
195 internal string FullPath {
197 if (fullpath == null) {
198 if (path == null || path == "")
199 fullpath = Environment.CurrentDirectory;
201 fullpath = System.IO.Path.GetFullPath (path);
208 [DefaultValue(false)]
209 [IODescription("Flag to indicate if this instance is active")]
210 public bool EnableRaisingEvents {
211 get { return enableRaisingEvents; }
213 if (value == enableRaisingEvents)
214 return; // Do nothing
216 enableRaisingEvents = value;
225 [DefaultValue("*.*")]
226 [IODescription("File name filter pattern")]
227 [RecommendedAsConfigurable(true)]
228 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
229 public string Filter {
230 get { return filter; }
232 if (value == null || value == "")
235 if (filter != value) {
238 mangledFilter = null;
243 [DefaultValue(false)]
244 [IODescription("Flag to indicate we want to watch subdirectories")]
245 public bool IncludeSubdirectories {
246 get { return includeSubdirectories; }
248 if (includeSubdirectories == value)
251 includeSubdirectories = value;
252 if (value && enableRaisingEvents) {
261 public int InternalBufferSize {
262 get { return internalBufferSize; }
264 if (internalBufferSize == value)
270 internalBufferSize = value;
271 if (enableRaisingEvents) {
278 [DefaultValue(NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite)]
279 [IODescription("Flag to indicate which change event we want to monitor")]
280 public NotifyFilters NotifyFilter {
281 get { return notifyFilter; }
283 if (notifyFilter == value)
286 notifyFilter = value;
287 if (enableRaisingEvents) {
295 [IODescription("The directory to monitor")]
296 [RecommendedAsConfigurable(true)]
297 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
298 [Editor ("System.Diagnostics.Design.FSWPathEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
306 Exception exc = null;
309 exists = Directory.Exists (value);
310 } catch (Exception e) {
315 throw new ArgumentException ("Invalid directory name", "value", exc);
318 throw new ArgumentException ("Directory does not exist", "value");
322 if (enableRaisingEvents) {
330 public override ISite Site {
331 get { return base.Site; }
332 set { base.Site = value; }
336 [IODescription("The object used to marshal the event handler calls resulting from a directory change")]
338 public ISynchronizeInvoke SynchronizingObject {
339 get { return synchronizingObject; }
340 set { synchronizingObject = value; }
343 #endregion // Properties
347 public void BeginInit ()
349 // Not necessary in Mono
352 protected override void Dispose (bool disposing)
359 base.Dispose (disposing);
362 ~FileSystemWatcher ()
368 public void EndInit ()
370 // Not necessary in Mono
378 private void RaiseEvent (Delegate ev, EventArgs arg, EventType evtype)
383 if (synchronizingObject == null) {
384 foreach (var target in ev.GetInvocationList()) {
386 case EventType.RenameEvent:
387 ((RenamedEventHandler)target).BeginInvoke (this, (RenamedEventArgs)arg, null, null);
389 case EventType.ErrorEvent:
390 ((ErrorEventHandler)target).BeginInvoke (this, (ErrorEventArgs)arg, null, null);
392 case EventType.FileSystemEvent:
393 ((FileSystemEventHandler)target).BeginInvoke (this, (FileSystemEventArgs)arg, null, null);
400 synchronizingObject.BeginInvoke (ev, new object [] {this, arg});
403 protected void OnChanged (FileSystemEventArgs e)
405 RaiseEvent (Changed, e, EventType.FileSystemEvent);
408 protected void OnCreated (FileSystemEventArgs e)
410 RaiseEvent (Created, e, EventType.FileSystemEvent);
413 protected void OnDeleted (FileSystemEventArgs e)
415 RaiseEvent (Deleted, e, EventType.FileSystemEvent);
418 protected void OnError (ErrorEventArgs e)
420 RaiseEvent (Error, e, EventType.ErrorEvent);
423 protected void OnRenamed (RenamedEventArgs e)
425 RaiseEvent (Renamed, e, EventType.RenameEvent);
428 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType)
430 return WaitForChanged (changeType, Timeout.Infinite);
433 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout)
435 WaitForChangedResult result = new WaitForChangedResult ();
436 bool prevEnabled = EnableRaisingEvents;
438 EnableRaisingEvents = true;
443 gotData = Monitor.Wait (this, timeout);
445 result = this.lastData;
448 EnableRaisingEvents = prevEnabled;
450 result.TimedOut = true;
455 internal void DispatchErrorEvents (ErrorEventArgs args)
460 internal void DispatchEvents (FileAction act, string filename, ref RenamedEventArgs renamed)
463 lastData = new WaitForChangedResult ();
467 case FileAction.Added:
468 lastData.Name = filename;
469 lastData.ChangeType = WatcherChangeTypes.Created;
470 OnCreated (new FileSystemEventArgs (WatcherChangeTypes.Created, path, filename));
472 case FileAction.Removed:
473 lastData.Name = filename;
474 lastData.ChangeType = WatcherChangeTypes.Deleted;
475 OnDeleted (new FileSystemEventArgs (WatcherChangeTypes.Deleted, path, filename));
477 case FileAction.Modified:
478 lastData.Name = filename;
479 lastData.ChangeType = WatcherChangeTypes.Changed;
480 OnChanged (new FileSystemEventArgs (WatcherChangeTypes.Changed, path, filename));
482 case FileAction.RenamedOldName:
483 if (renamed != null) {
486 lastData.OldName = filename;
487 lastData.ChangeType = WatcherChangeTypes.Renamed;
488 renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, filename, "");
490 case FileAction.RenamedNewName:
491 lastData.Name = filename;
492 lastData.ChangeType = WatcherChangeTypes.Renamed;
493 if (renamed == null) {
494 renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, "", filename);
506 watcher.StartDispatching (this);
511 watcher.StopDispatching (this);
513 #endregion // Methods
515 #region Events and Delegates
517 [IODescription("Occurs when a file/directory change matches the filter")]
518 public event FileSystemEventHandler Changed;
520 [IODescription("Occurs when a file/directory creation matches the filter")]
521 public event FileSystemEventHandler Created;
523 [IODescription("Occurs when a file/directory deletion matches the filter")]
524 public event FileSystemEventHandler Deleted;
527 public event ErrorEventHandler Error;
529 [IODescription("Occurs when a file/directory rename matches the filter")]
530 public event RenamedEventHandler Renamed;
532 #endregion // Events and Delegates
534 /* 0 -> not supported */
540 [MethodImplAttribute(MethodImplOptions.InternalCall)]
541 static extern int InternalSupportsFSW ();