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
379 protected void OnChanged (FileSystemEventArgs e)
384 if (synchronizingObject == null)
387 synchronizingObject.BeginInvoke (Changed, new object[] { this, e });
390 protected void OnCreated (FileSystemEventArgs e)
395 if (synchronizingObject == null)
398 synchronizingObject.BeginInvoke (Created, new object[] { this, e });
401 protected void OnDeleted (FileSystemEventArgs e)
406 if (synchronizingObject == null)
409 synchronizingObject.BeginInvoke (Deleted, new object[] { this, e });
412 internal void OnError (ErrorEventArgs e)
417 if (synchronizingObject == null)
420 synchronizingObject.BeginInvoke (Error, new object[] { this, e });
423 protected void OnRenamed (RenamedEventArgs e)
428 if (synchronizingObject == null)
431 synchronizingObject.BeginInvoke (Renamed, new object[] { this, e });
434 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType)
436 return WaitForChanged (changeType, Timeout.Infinite);
439 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout)
441 WaitForChangedResult result = new WaitForChangedResult ();
442 bool prevEnabled = EnableRaisingEvents;
444 EnableRaisingEvents = true;
449 gotData = Monitor.Wait (this, timeout);
451 result = this.lastData;
454 EnableRaisingEvents = prevEnabled;
456 result.TimedOut = true;
461 internal void DispatchEvents (FileAction act, string filename, ref RenamedEventArgs renamed)
464 lastData = new WaitForChangedResult ();
468 case FileAction.Added:
469 lastData.Name = filename;
470 lastData.ChangeType = WatcherChangeTypes.Created;
471 OnCreated (new FileSystemEventArgs (WatcherChangeTypes.Created, path, filename));
473 case FileAction.Removed:
474 lastData.Name = filename;
475 lastData.ChangeType = WatcherChangeTypes.Deleted;
476 OnDeleted (new FileSystemEventArgs (WatcherChangeTypes.Deleted, path, filename));
478 case FileAction.Modified:
479 lastData.Name = filename;
480 lastData.ChangeType = WatcherChangeTypes.Changed;
481 OnChanged (new FileSystemEventArgs (WatcherChangeTypes.Changed, path, filename));
483 case FileAction.RenamedOldName:
484 if (renamed != null) {
487 lastData.OldName = filename;
488 lastData.ChangeType = WatcherChangeTypes.Renamed;
489 renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, filename, "");
491 case FileAction.RenamedNewName:
492 lastData.Name = filename;
493 lastData.ChangeType = WatcherChangeTypes.Renamed;
494 if (renamed == null) {
495 renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, "", filename);
507 watcher.StartDispatching (this);
512 watcher.StopDispatching (this);
514 #endregion // Methods
516 #region Events and Delegates
518 [IODescription("Occurs when a file/directory change matches the filter")]
519 public event FileSystemEventHandler Changed;
521 [IODescription("Occurs when a file/directory creation matches the filter")]
522 public event FileSystemEventHandler Created;
524 [IODescription("Occurs when a file/directory deletion matches the filter")]
525 public event FileSystemEventHandler Deleted;
528 public event ErrorEventHandler Error;
530 [IODescription("Occurs when a file/directory rename matches the filter")]
531 public event RenamedEventHandler Renamed;
533 #endregion // Events and Delegates
535 /* 0 -> not supported */
541 [MethodImplAttribute(MethodImplOptions.InternalCall)]
542 static extern int InternalSupportsFSW ();