2 // System.IO.FileSystemWatcher.cs
\r
5 // Tim Coleman (tim@timcoleman.com)
\r
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
\r
8 // Copyright (C) Tim Coleman, 2002
\r
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
\r
13 using System.ComponentModel;
\r
14 using System.Threading;
\r
16 namespace System.IO {
\r
17 [DefaultEvent("Changed")]
\r
18 public class FileSystemWatcher : Component, ISupportInitialize {
\r
22 bool enableRaisingEvents;
\r
24 bool includeSubdirectories;
\r
25 int internalBufferSize;
\r
26 NotifyFilters notifyFilter;
\r
29 ISynchronizeInvoke synchronizingObject;
\r
31 #endregion // Fields
\r
33 #region Constructors
\r
35 public FileSystemWatcher ()
\r
37 this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
\r
38 this.enableRaisingEvents = false;
\r
39 this.filter = "*.*";
\r
40 this.includeSubdirectories = false;
\r
41 this.internalBufferSize = 8192;
\r
45 public FileSystemWatcher (string path)
\r
46 : this (path, String.Empty)
\r
50 public FileSystemWatcher (string path, string filter)
\r
53 throw new ArgumentNullException ("path");
\r
56 throw new ArgumentNullException ("filter");
\r
58 if (path == String.Empty)
\r
59 throw new ArgumentException ("Empty path", "path");
\r
61 if (!Directory.Exists (path))
\r
62 throw new ArgumentException ("Directory does not exists", "path");
\r
64 this.enableRaisingEvents = false;
\r
65 this.filter = filter;
\r
66 this.includeSubdirectories = false;
\r
67 this.internalBufferSize = 8192;
\r
68 this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
\r
70 this.synchronizingObject = null;
\r
73 #endregion // Constructors
\r
77 [DefaultValue(false)]
\r
78 [IODescription("Flag to indicate if this instance is active")]
\r
79 public bool EnableRaisingEvents {
\r
80 get { return enableRaisingEvents; }
\r
81 set { enableRaisingEvents = value; }
\r
84 [DefaultValue("*.*")]
\r
85 [IODescription("File name filter pattern")]
\r
86 [RecommendedAsConfigurable(true)]
87 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
\r
88 public string Filter {
\r
89 get { return filter; }
\r
92 if (filter == null || filter == "")
\r
97 [DefaultValue(false)]
\r
98 [IODescription("Flag to indicate we want to watch subdirectories")]
\r
99 public bool IncludeSubdirectories {
\r
100 get { return includeSubdirectories; }
\r
101 set { includeSubdirectories = value; }
\r
105 [DefaultValue(8192)]
\r
106 public int InternalBufferSize {
\r
107 get { return internalBufferSize; }
\r
108 set { internalBufferSize = value; }
\r
111 [DefaultValue(NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite)]
\r
112 [IODescription("Flag to indicate which change event we want to monitor")]
\r
113 public NotifyFilters NotifyFilter {
\r
114 get { return notifyFilter; }
\r
115 [MonoTODO ("Perform validation.")]
\r
116 set { notifyFilter = value; }
\r
120 [IODescription("The directory to monitor")]
\r
121 [RecommendedAsConfigurable(true)]
122 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
123 [Editor ("System.Diagnostics.Design.FSWPathEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
\r
124 public string Path {
\r
125 get { return path; }
\r
127 bool exists = false;
\r
128 Exception exc = null;
\r
131 exists = Directory.Exists (value);
\r
132 } catch (Exception e) {
\r
138 throw new ArgumentException ("Invalid directory name", "value", exc);
\r
141 throw new ArgumentException ("Directory does not exists", "value");
\r
148 public override ISite Site {
\r
149 get { return site; }
\r
150 set { site = value; }
\r
153 [DefaultValue(null)]
\r
154 [IODescription("The object used to marshal the event handler calls resulting from a directory change")]
\r
155 public ISynchronizeInvoke SynchronizingObject {
\r
156 get { return synchronizingObject; }
\r
157 set { synchronizingObject = value; }
\r
160 #endregion // Properties
\r
165 public void BeginInit ()
\r
167 throw new NotImplementedException ();
\r
171 protected override void Dispose (bool disposing)
\r
176 base.Dispose (disposing);
\r
180 public void EndInit ()
\r
182 throw new NotImplementedException ();
\r
185 private void RaiseEvent (Delegate ev, EventArgs arg)
\r
190 object [] args = new object [] {this, arg};
\r
192 if (synchronizingObject == null) {
\r
193 ev.DynamicInvoke (args);
\r
197 synchronizingObject.BeginInvoke (ev, args);
\r
200 protected void OnChanged (FileSystemEventArgs e)
\r
202 RaiseEvent (Changed, e);
\r
205 protected void OnCreated (FileSystemEventArgs e)
\r
207 RaiseEvent (Created, e);
\r
210 protected void OnDeleted (FileSystemEventArgs e)
\r
212 RaiseEvent (Deleted, e);
\r
215 protected void OnError (ErrorEventArgs e)
\r
217 RaiseEvent (Error, e);
\r
220 protected void OnRenamed (RenamedEventArgs e)
\r
222 RaiseEvent (Renamed, e);
\r
225 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType)
\r
227 return WaitForChanged (changeType, Timeout.Infinite);
\r
231 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout)
\r
233 throw new NotImplementedException ();
\r
236 #endregion // Methods
\r
238 #region Events and Delegates
\r
240 [IODescription("Occurs when a file/directory change matches the filter")]
\r
241 public event FileSystemEventHandler Changed;
\r
244 [IODescription("Occurs when a file/directory creation matches the filter")]
\r
245 public event FileSystemEventHandler Created;
\r
247 [IODescription("Occurs when a file/directory deletion matches the filter")]
\r
248 public event FileSystemEventHandler Deleted;
\r
251 public event ErrorEventHandler Error;
\r
253 [IODescription("Occurs when a file/directory rename matches the filter")]
\r
254 public event RenamedEventHandler Renamed;
\r
256 #endregion // Events and Delegates
\r