2003-07-17 Andreas Nahr <ClassDevelopment@A-SoftTech.com>
[mono.git] / mcs / class / System / System.IO / FileSystemWatcher.cs
1 // \r
2 // System.IO.FileSystemWatcher.cs\r
3 //\r
4 // Authors:\r
5 //      Tim Coleman (tim@timcoleman.com)\r
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)\r
7 //\r
8 // Copyright (C) Tim Coleman, 2002 \r
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)\r
10 //\r
11 \r
12 using System;\r
13 using System.ComponentModel;\r
14 using System.Threading;\r
15 \r
16 namespace System.IO {\r
17         [DefaultEvent("Changed")]\r
18         public class FileSystemWatcher : Component, ISupportInitialize {\r
19 \r
20                 #region Fields\r
21 \r
22                 bool enableRaisingEvents;\r
23                 string filter;\r
24                 bool includeSubdirectories;\r
25                 int internalBufferSize;\r
26                 NotifyFilters notifyFilter;\r
27                 string path;\r
28                 ISite site;\r
29                 ISynchronizeInvoke synchronizingObject;\r
30 \r
31                 #endregion // Fields\r
32 \r
33                 #region Constructors\r
34 \r
35                 public FileSystemWatcher ()\r
36                 {\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
42                         this.path = "";\r
43                 }\r
44 \r
45                 public FileSystemWatcher (string path)\r
46                         : this (path, String.Empty)\r
47                 {\r
48                 }\r
49 \r
50                 public FileSystemWatcher (string path, string filter)\r
51                 {\r
52                         if (path == null)\r
53                                 throw new ArgumentNullException ("path");\r
54 \r
55                         if (filter == null)\r
56                                 throw new ArgumentNullException ("filter");\r
57 \r
58                         if (path == String.Empty)\r
59                                 throw new ArgumentException ("Empty path", "path");\r
60 \r
61                         if (!Directory.Exists (path))\r
62                                 throw new ArgumentException ("Directory does not exists", "path");\r
63 \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
69                         this.path = path;\r
70                         this.synchronizingObject = null;\r
71                 }\r
72 \r
73                 #endregion // Constructors\r
74 \r
75                 #region Properties\r
76 \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
82                 }\r
83 \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
90                         set {\r
91                                 filter = value;\r
92                                 if (filter == null || filter == "")\r
93                                         filter = "*.*";\r
94                         }\r
95                 }\r
96 \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
102                 }\r
103 \r
104                 [Browsable(false)]\r
105                 [DefaultValue(8192)]\r
106                 public int InternalBufferSize {\r
107                         get { return internalBufferSize; }\r
108                         set { internalBufferSize = value; }\r
109                 }\r
110 \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
117                 }\r
118 \r
119                 [DefaultValue("")]\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
126                         set {\r
127                                 bool exists = false;\r
128                                 Exception exc = null;\r
129 \r
130                                 try {\r
131                                         exists = Directory.Exists (value);\r
132                                 } catch (Exception e) {\r
133                                         exists = false;\r
134                                         exc = e;\r
135                                 }\r
136 \r
137                                 if (exc != null)\r
138                                         throw new ArgumentException ("Invalid directory name", "value", exc);\r
139 \r
140                                 if (!exists)\r
141                                         throw new ArgumentException ("Directory does not exists", "value");\r
142 \r
143                                 path = value;\r
144                         }\r
145                 }\r
146 \r
147                 [Browsable(false)]\r
148                 public override ISite Site {\r
149                         get { return site; }\r
150                         set { site = value; }\r
151                 }\r
152 \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
158                 }\r
159 \r
160                 #endregion // Properties\r
161 \r
162                 #region Methods\r
163         \r
164                 [MonoTODO]\r
165                 public void BeginInit ()\r
166                 {\r
167                         throw new NotImplementedException (); \r
168                 }\r
169 \r
170                 [MonoTODO]\r
171                 protected override void Dispose (bool disposing)\r
172                 {\r
173                         if (disposing) {\r
174                                 // \r
175                         }\r
176                         base.Dispose (disposing);\r
177                 }\r
178 \r
179                 [MonoTODO]\r
180                 public void EndInit ()\r
181                 {\r
182                         throw new NotImplementedException (); \r
183                 }\r
184 \r
185                 private void RaiseEvent (Delegate ev, EventArgs arg)\r
186                 {\r
187                         if (ev == null)\r
188                                 return;\r
189 \r
190                         object [] args = new object [] {this, arg};\r
191 \r
192                         if (synchronizingObject == null) {\r
193                                 ev.DynamicInvoke (args);\r
194                                 return;\r
195                         }\r
196                         \r
197                         synchronizingObject.BeginInvoke (ev, args);\r
198                 }\r
199 \r
200                 protected void OnChanged (FileSystemEventArgs e)\r
201                 {\r
202                         RaiseEvent (Changed, e);\r
203                 }\r
204 \r
205                 protected void OnCreated (FileSystemEventArgs e)\r
206                 {\r
207                         RaiseEvent (Created, e);\r
208                 }\r
209 \r
210                 protected void OnDeleted (FileSystemEventArgs e)\r
211                 {\r
212                         RaiseEvent (Deleted, e);\r
213                 }\r
214 \r
215                 protected void OnError (ErrorEventArgs e)\r
216                 {\r
217                         RaiseEvent (Error, e);\r
218                 }\r
219 \r
220                 protected void OnRenamed (RenamedEventArgs e)\r
221                 {\r
222                         RaiseEvent (Renamed, e);\r
223                 }\r
224 \r
225                 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType)\r
226                 {\r
227                         return WaitForChanged (changeType, Timeout.Infinite);\r
228                 }\r
229 \r
230                 [MonoTODO]\r
231                 public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout)\r
232                 {\r
233                         throw new NotImplementedException (); \r
234                 }\r
235 \r
236                 #endregion // Methods\r
237 \r
238                 #region Events and Delegates\r
239 \r
240                 [IODescription("Occurs when a file/directory change matches the filter")]\r
241                 public event FileSystemEventHandler Changed;\r
242 \r
243 \r
244                 [IODescription("Occurs when a file/directory creation matches the filter")]\r
245                 public event FileSystemEventHandler Created;\r
246 \r
247                 [IODescription("Occurs when a file/directory deletion matches the filter")]\r
248                 public event FileSystemEventHandler Deleted;\r
249 \r
250                 [Browsable(false)]\r
251                 public event ErrorEventHandler Error;\r
252 \r
253                 [IODescription("Occurs when a file/directory rename matches the filter")]\r
254                 public event RenamedEventHandler Renamed;\r
255 \r
256                 #endregion // Events and Delegates\r
257         }\r
258 }\r