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 // (c) 2004 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.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Security.Permissions;
38 using System.Threading;
41 [DefaultEvent("Changed")]
42 public class FileSystemWatcher : Component, ISupportInitialize {
46 bool enableRaisingEvents;
48 bool includeSubdirectories;
49 int internalBufferSize;
50 NotifyFilters notifyFilter;
53 ISynchronizeInvoke synchronizingObject;
54 WaitForChangedResult lastData;
56 SearchPattern2 pattern;
59 static IFileWatcher watcher;
60 static object lockobj = new object ();
66 public FileSystemWatcher ()
68 this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
69 this.enableRaisingEvents = false;
71 this.includeSubdirectories = false;
72 this.internalBufferSize = 8192;
77 public FileSystemWatcher (string path)
82 public FileSystemWatcher (string path, string filter)
85 throw new ArgumentNullException ("path");
88 throw new ArgumentNullException ("filter");
90 if (path == String.Empty)
91 throw new ArgumentException ("Empty path", "path");
93 if (!Directory.Exists (path))
94 throw new ArgumentException ("Directory does not exists", "path");
96 this.enableRaisingEvents = false;
98 this.includeSubdirectories = false;
99 this.internalBufferSize = 8192;
100 this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
102 this.synchronizingObject = null;
106 [EnvironmentPermission (SecurityAction.Assert, Read="MONO_MANAGED_WATCHER")]
113 string managed = Environment.GetEnvironmentVariable ("MONO_MANAGED_WATCHER");
116 mode = InternalSupportsFSW ();
121 ok = DefaultWatcher.GetInstance (out watcher);
122 //ok = WindowsWatcher.GetInstance (out watcher);
125 ok = FAMWatcher.GetInstance (out watcher, false);
128 ok = KeventWatcher.GetInstance (out watcher);
131 ok = FAMWatcher.GetInstance (out watcher, true);
134 ok = InotifyWatcher.GetInstance (out watcher, true);
138 if (mode == 0 || !ok)
139 DefaultWatcher.GetInstance (out watcher);
143 #endregion // Constructors
147 /* If this is enabled, we Pulse this instance */
148 internal bool Waiting {
149 get { return waiting; }
150 set { waiting = value; }
153 internal string MangledFilter {
158 if (mangledFilter != null)
159 return mangledFilter;
161 string filterLocal = "*.*";
162 if (!(watcher.GetType () == typeof (WindowsWatcher)))
169 internal SearchPattern2 Pattern {
171 if (pattern == null) {
172 pattern = new SearchPattern2 (MangledFilter);
178 internal string FullPath {
180 if (fullpath == null) {
181 if (path == null || path == "")
182 fullpath = Environment.CurrentDirectory;
184 fullpath = System.IO.Path.GetFullPath (path);
191 [DefaultValue(false)]
192 [IODescription("Flag to indicate if this instance is active")]
193 public bool EnableRaisingEvents {
194 get { return enableRaisingEvents; }
196 if (value == enableRaisingEvents)
197 return; // Do nothing
199 enableRaisingEvents = value;
208 [DefaultValue("*.*")]
209 [IODescription("File name filter pattern")]
210 [RecommendedAsConfigurable(true)]
211 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
212 public string Filter {
213 get { return filter; }
215 if (value == null || value == "")
218 if (filter != value) {
221 mangledFilter = null;
226 [DefaultValue(false)]
227 [IODescription("Flag to indicate we want to watch subdirectories")]
228 public bool IncludeSubdirectories {
229 get { return includeSubdirectories; }
231 if (includeSubdirectories == value)
234 includeSubdirectories = value;
235 if (value && enableRaisingEvents) {
244 public int InternalBufferSize {
245 get { return internalBufferSize; }
247 if (internalBufferSize == value)
253 internalBufferSize = value;
254 if (enableRaisingEvents) {
261 [DefaultValue(NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite)]
262 [IODescription("Flag to indicate which change event we want to monitor")]
263 public NotifyFilters NotifyFilter {
264 get { return notifyFilter; }
266 if (notifyFilter == value)
269 notifyFilter = value;
270 if (enableRaisingEvents) {
278 [IODescription("The directory to monitor")]
279 [RecommendedAsConfigurable(true)]
280 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
281 [Editor ("System.Diagnostics.Design.FSWPathEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
289 Exception exc = null;
292 exists = Directory.Exists (value);
293 } catch (Exception e) {
298 throw new ArgumentException ("Invalid directory name", "value", exc);
301 throw new ArgumentException ("Directory does not exists", "value");
305 if (enableRaisingEvents) {
313 public override ISite Site {
314 get { return base.Site; }
315 set { base.Site = value; }
319 [IODescription("The object used to marshal the event handler calls resulting from a directory change")]
320 public ISynchronizeInvoke SynchronizingObject {
321 get { return synchronizingObject; }
322 set { synchronizingObject = value; }
325 #endregion // Properties
330 public void BeginInit ()
332 throw new NotImplementedException ();
335 protected override void Dispose (bool disposing)
342 base.Dispose (disposing);
345 ~FileSystemWatcher ()
352 public void EndInit ()
354 throw new NotImplementedException ();
362 private void RaiseEvent (Delegate ev, EventArgs arg, EventType evtype)
367 if (synchronizingObject == null) {
368 Delegate [] delegates = ev.GetInvocationList ();
369 if (evtype == EventType.RenameEvent) {
370 foreach (RenamedEventHandler d in delegates){
371 d.BeginInvoke (this, (RenamedEventArgs) arg, null, null);
373 } else if (evtype == EventType.ErrorEvent) {
374 foreach (ErrorEventHandler d in delegates){
375 d.BeginInvoke (this, (ErrorEventArgs) arg, null, null);
378 foreach (FileSystemEventHandler d in delegates){
379 d.BeginInvoke (this, (FileSystemEventArgs) arg, null, null);
385 synchronizingObject.BeginInvoke (ev, new object [] {this, arg});
388 protected void OnChanged (FileSystemEventArgs e)
390 RaiseEvent (Changed, e, EventType.FileSystemEvent);
393 protected void OnCreated (FileSystemEventArgs e)
395 RaiseEvent (Created, e, EventType.FileSystemEvent);
398 protected void OnDeleted (FileSystemEventArgs e)
400 RaiseEvent (Deleted, e, EventType.FileSystemEvent);
403 protected void OnError (ErrorEventArgs e)
405 RaiseEvent (Error, e, EventType.ErrorEvent);
408 protected void OnRenamed (RenamedEventArgs e)
410 RaiseEvent (Renamed, e, EventType.RenameEvent);
413 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType)
415 return WaitForChanged (changeType, Timeout.Infinite);
418 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout)
420 WaitForChangedResult result = new WaitForChangedResult ();
421 bool prevEnabled = EnableRaisingEvents;
423 EnableRaisingEvents = true;
428 gotData = Monitor.Wait (this, timeout);
430 result = this.lastData;
433 EnableRaisingEvents = prevEnabled;
435 result.TimedOut = true;
440 internal void DispatchEvents (FileAction act, string filename, ref RenamedEventArgs renamed)
443 lastData = new WaitForChangedResult ();
447 case FileAction.Added:
448 lastData.Name = filename;
449 lastData.ChangeType = WatcherChangeTypes.Created;
450 OnCreated (new FileSystemEventArgs (WatcherChangeTypes.Created, path, filename));
452 case FileAction.Removed:
453 lastData.Name = filename;
454 lastData.ChangeType = WatcherChangeTypes.Deleted;
455 OnDeleted (new FileSystemEventArgs (WatcherChangeTypes.Deleted, path, filename));
457 case FileAction.Modified:
458 lastData.Name = filename;
459 lastData.ChangeType = WatcherChangeTypes.Changed;
460 OnChanged (new FileSystemEventArgs (WatcherChangeTypes.Changed, path, filename));
462 case FileAction.RenamedOldName:
463 if (renamed != null) {
466 lastData.OldName = filename;
467 lastData.ChangeType = WatcherChangeTypes.Renamed;
468 renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, filename, "");
470 case FileAction.RenamedNewName:
471 lastData.Name = filename;
472 lastData.ChangeType = WatcherChangeTypes.Renamed;
473 if (renamed == null) {
474 renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, "", filename);
486 watcher.StartDispatching (this);
491 watcher.StopDispatching (this);
493 #endregion // Methods
495 #region Events and Delegates
497 [IODescription("Occurs when a file/directory change matches the filter")]
498 public event FileSystemEventHandler Changed;
500 [IODescription("Occurs when a file/directory creation matches the filter")]
501 public event FileSystemEventHandler Created;
503 [IODescription("Occurs when a file/directory deletion matches the filter")]
504 public event FileSystemEventHandler Deleted;
507 public event ErrorEventHandler Error;
509 [IODescription("Occurs when a file/directory rename matches the filter")]
510 public event RenamedEventHandler Renamed;
512 #endregion // Events and Delegates
514 /* 0 -> not supported */
520 [MethodImplAttribute(MethodImplOptions.InternalCall)]
521 static extern int InternalSupportsFSW ();