FileSystemWatcher.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. //
  2. // System.IO.FileSystemWatcher.cs
  3. //
  4. // Authors:
  5. // Tim Coleman ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  10. // (c) 2004 Novell, Inc. (http://www.novell.com)
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.ComponentModel;
  34. using System.Runtime.CompilerServices;
  35. using System.Runtime.InteropServices;
  36. using System.Threading;
  37. namespace System.IO {
  38. [DefaultEvent("Changed")]
  39. public class FileSystemWatcher : Component, ISupportInitialize {
  40. #region Fields
  41. bool enableRaisingEvents;
  42. string filter;
  43. bool includeSubdirectories;
  44. int internalBufferSize;
  45. NotifyFilters notifyFilter;
  46. string path;
  47. string fullpath;
  48. ISynchronizeInvoke synchronizingObject;
  49. WaitForChangedResult lastData;
  50. bool waiting;
  51. SearchPattern2 pattern;
  52. bool disposed;
  53. string mangledFilter;
  54. static IFileWatcher watcher;
  55. #endregion // Fields
  56. #region Constructors
  57. public FileSystemWatcher ()
  58. {
  59. this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  60. this.enableRaisingEvents = false;
  61. this.filter = "*.*";
  62. this.includeSubdirectories = false;
  63. this.internalBufferSize = 8192;
  64. this.path = "";
  65. InitWatcher ();
  66. }
  67. public FileSystemWatcher (string path)
  68. : this (path, "*.*")
  69. {
  70. }
  71. public FileSystemWatcher (string path, string filter)
  72. {
  73. if (path == null)
  74. throw new ArgumentNullException ("path");
  75. if (filter == null)
  76. throw new ArgumentNullException ("filter");
  77. if (path == String.Empty)
  78. throw new ArgumentException ("Empty path", "path");
  79. if (!Directory.Exists (path))
  80. throw new ArgumentException ("Directory does not exists", "path");
  81. this.enableRaisingEvents = false;
  82. this.filter = filter;
  83. this.includeSubdirectories = false;
  84. this.internalBufferSize = 8192;
  85. this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  86. this.path = path;
  87. this.synchronizingObject = null;
  88. InitWatcher ();
  89. }
  90. void InitWatcher ()
  91. {
  92. lock (typeof (FileSystemWatcher)) {
  93. if (watcher != null)
  94. return;
  95. string managed = Environment.GetEnvironmentVariable ("MONO_MANAGED_WATCHER");
  96. int mode = 0;
  97. if (managed == null)
  98. mode = InternalSupportsFSW ();
  99. bool ok = false;
  100. if (mode == 3)
  101. ok = KeventWatcher.GetInstance (out watcher);
  102. else if (mode == 2)
  103. ok = FAMWatcher.GetInstance (out watcher);
  104. else if (mode == 1)
  105. ok = DefaultWatcher.GetInstance (out watcher);
  106. //ok = WindowsWatcher.GetInstance (out watcher);
  107. if (mode == 0 || !ok)
  108. DefaultWatcher.GetInstance (out watcher);
  109. }
  110. }
  111. #endregion // Constructors
  112. #region Properties
  113. /* If this is enabled, we Pulse this instance */
  114. internal bool Waiting {
  115. get { return waiting; }
  116. set { waiting = value; }
  117. }
  118. internal string MangledFilter {
  119. get {
  120. if (filter != "*.*")
  121. return filter;
  122. if (mangledFilter != null)
  123. return mangledFilter;
  124. string filterLocal = "*.*";
  125. if (!(watcher.GetType () == typeof (WindowsWatcher)))
  126. filterLocal = "*";
  127. return filterLocal;
  128. }
  129. }
  130. internal SearchPattern2 Pattern {
  131. get {
  132. if (pattern == null) {
  133. pattern = new SearchPattern2 (MangledFilter);
  134. }
  135. return pattern;
  136. }
  137. }
  138. internal string FullPath {
  139. get {
  140. if (fullpath == null) {
  141. if (path == null || path == "")
  142. fullpath = Environment.CurrentDirectory;
  143. else
  144. fullpath = System.IO.Path.GetFullPath (path);
  145. }
  146. return fullpath;
  147. }
  148. }
  149. [DefaultValue(false)]
  150. [IODescription("Flag to indicate if this instance is active")]
  151. public bool EnableRaisingEvents {
  152. get { return enableRaisingEvents; }
  153. set {
  154. if (value == enableRaisingEvents)
  155. return; // Do nothing
  156. enableRaisingEvents = value;
  157. if (value) {
  158. Start ();
  159. } else {
  160. Stop ();
  161. }
  162. }
  163. }
  164. [DefaultValue("*.*")]
  165. [IODescription("File name filter pattern")]
  166. [RecommendedAsConfigurable(true)]
  167. [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
  168. public string Filter {
  169. get { return filter; }
  170. set {
  171. if (value == null || value == "")
  172. value = "*.*";
  173. if (filter != value) {
  174. filter = value;
  175. pattern = null;
  176. mangledFilter = null;
  177. }
  178. }
  179. }
  180. [DefaultValue(false)]
  181. [IODescription("Flag to indicate we want to watch subdirectories")]
  182. public bool IncludeSubdirectories {
  183. get { return includeSubdirectories; }
  184. set {
  185. if (includeSubdirectories == value)
  186. return;
  187. includeSubdirectories = value;
  188. if (value && enableRaisingEvents) {
  189. Stop ();
  190. Start ();
  191. }
  192. }
  193. }
  194. [Browsable(false)]
  195. [DefaultValue(8192)]
  196. public int InternalBufferSize {
  197. get { return internalBufferSize; }
  198. set {
  199. if (internalBufferSize == value)
  200. return;
  201. if (value < 4196)
  202. value = 4196;
  203. internalBufferSize = value;
  204. if (enableRaisingEvents) {
  205. Stop ();
  206. Start ();
  207. }
  208. }
  209. }
  210. [DefaultValue(NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite)]
  211. [IODescription("Flag to indicate which change event we want to monitor")]
  212. public NotifyFilters NotifyFilter {
  213. get { return notifyFilter; }
  214. set {
  215. if (notifyFilter == value)
  216. return;
  217. notifyFilter = value;
  218. if (enableRaisingEvents) {
  219. Stop ();
  220. Start ();
  221. }
  222. }
  223. }
  224. [DefaultValue("")]
  225. [IODescription("The directory to monitor")]
  226. [RecommendedAsConfigurable(true)]
  227. [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
  228. [Editor ("System.Diagnostics.Design.FSWPathEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  229. public string Path {
  230. get { return path; }
  231. set {
  232. if (path == value)
  233. return;
  234. bool exists = false;
  235. Exception exc = null;
  236. try {
  237. exists = Directory.Exists (value);
  238. } catch (Exception e) {
  239. exc = e;
  240. }
  241. if (exc != null)
  242. throw new ArgumentException ("Invalid directory name", "value", exc);
  243. if (!exists)
  244. throw new ArgumentException ("Directory does not exists", "value");
  245. path = value;
  246. fullpath = null;
  247. if (enableRaisingEvents) {
  248. Stop ();
  249. Start ();
  250. }
  251. }
  252. }
  253. [Browsable(false)]
  254. public override ISite Site {
  255. get { return base.Site; }
  256. set { base.Site = value; }
  257. }
  258. [DefaultValue(null)]
  259. [IODescription("The object used to marshal the event handler calls resulting from a directory change")]
  260. public ISynchronizeInvoke SynchronizingObject {
  261. get { return synchronizingObject; }
  262. set { synchronizingObject = value; }
  263. }
  264. #endregion // Properties
  265. #region Methods
  266. [MonoTODO]
  267. public void BeginInit ()
  268. {
  269. throw new NotImplementedException ();
  270. }
  271. protected override void Dispose (bool disposing)
  272. {
  273. if (!disposed) {
  274. disposed = true;
  275. Stop ();
  276. }
  277. base.Dispose (disposing);
  278. }
  279. ~FileSystemWatcher ()
  280. {
  281. disposed = true;
  282. Stop ();
  283. }
  284. [MonoTODO]
  285. public void EndInit ()
  286. {
  287. throw new NotImplementedException ();
  288. }
  289. private void RaiseEvent (Delegate ev, EventArgs arg)
  290. {
  291. if (ev == null)
  292. return;
  293. object [] args = new object [] {this, arg};
  294. if (synchronizingObject == null) {
  295. ev.DynamicInvoke (args);
  296. return;
  297. }
  298. synchronizingObject.BeginInvoke (ev, args);
  299. }
  300. protected void OnChanged (FileSystemEventArgs e)
  301. {
  302. RaiseEvent (Changed, e);
  303. }
  304. protected void OnCreated (FileSystemEventArgs e)
  305. {
  306. RaiseEvent (Created, e);
  307. }
  308. protected void OnDeleted (FileSystemEventArgs e)
  309. {
  310. RaiseEvent (Deleted, e);
  311. }
  312. protected void OnError (ErrorEventArgs e)
  313. {
  314. RaiseEvent (Error, e);
  315. }
  316. protected void OnRenamed (RenamedEventArgs e)
  317. {
  318. RaiseEvent (Renamed, e);
  319. }
  320. public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType)
  321. {
  322. return WaitForChanged (changeType, Timeout.Infinite);
  323. }
  324. public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout)
  325. {
  326. WaitForChangedResult result = new WaitForChangedResult ();
  327. bool prevEnabled = EnableRaisingEvents;
  328. if (!prevEnabled)
  329. EnableRaisingEvents = true;
  330. bool gotData;
  331. lock (this) {
  332. waiting = true;
  333. gotData = Monitor.Wait (this, timeout);
  334. if (gotData)
  335. result = this.lastData;
  336. }
  337. EnableRaisingEvents = prevEnabled;
  338. if (!gotData)
  339. result.TimedOut = true;
  340. return result;
  341. }
  342. internal void DispatchEvents (FileAction act, string filename, ref RenamedEventArgs renamed)
  343. {
  344. if (waiting) {
  345. lastData = new WaitForChangedResult ();
  346. }
  347. switch (act) {
  348. case FileAction.Added:
  349. lastData.Name = filename;
  350. lastData.ChangeType = WatcherChangeTypes.Created;
  351. OnCreated (new FileSystemEventArgs (WatcherChangeTypes.Created, path, filename));
  352. break;
  353. case FileAction.Removed:
  354. lastData.Name = filename;
  355. lastData.ChangeType = WatcherChangeTypes.Deleted;
  356. OnDeleted (new FileSystemEventArgs (WatcherChangeTypes.Deleted, path, filename));
  357. break;
  358. case FileAction.Modified:
  359. lastData.Name = filename;
  360. lastData.ChangeType = WatcherChangeTypes.Changed;
  361. OnChanged (new FileSystemEventArgs (WatcherChangeTypes.Changed, path, filename));
  362. break;
  363. case FileAction.RenamedOldName:
  364. if (renamed != null) {
  365. OnRenamed (renamed);
  366. }
  367. lastData.OldName = filename;
  368. lastData.ChangeType = WatcherChangeTypes.Renamed;
  369. renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, null, filename);
  370. break;
  371. case FileAction.RenamedNewName:
  372. lastData.Name = filename;
  373. lastData.ChangeType = WatcherChangeTypes.Renamed;
  374. if (renamed != null) {
  375. renamed.SetName (filename);
  376. } else {
  377. renamed = new RenamedEventArgs (WatcherChangeTypes.Renamed, path, filename, null);
  378. }
  379. OnRenamed (renamed);
  380. renamed = null;
  381. break;
  382. default:
  383. break;
  384. }
  385. }
  386. void Start ()
  387. {
  388. watcher.StartDispatching (this);
  389. }
  390. void Stop ()
  391. {
  392. watcher.StopDispatching (this);
  393. }
  394. #endregion // Methods
  395. #region Events and Delegates
  396. [IODescription("Occurs when a file/directory change matches the filter")]
  397. public event FileSystemEventHandler Changed;
  398. [IODescription("Occurs when a file/directory creation matches the filter")]
  399. public event FileSystemEventHandler Created;
  400. [IODescription("Occurs when a file/directory deletion matches the filter")]
  401. public event FileSystemEventHandler Deleted;
  402. [Browsable(false)]
  403. public event ErrorEventHandler Error;
  404. [IODescription("Occurs when a file/directory rename matches the filter")]
  405. public event RenamedEventHandler Renamed;
  406. #endregion // Events and Delegates
  407. /* 0 -> not supported */
  408. /* 1 -> windows */
  409. /* 2 -> FAM */
  410. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  411. static extern int InternalSupportsFSW ();
  412. /*[MethodImplAttribute(MethodImplOptions.InternalCall)]
  413. static extern IntPtr InternalOpenDirectory (string path, IntPtr reserved);
  414. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  415. static extern IntPtr InternalCloseDirectory (IntPtr handle);
  416. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  417. static extern bool InternalReadDirectoryChanges (IntPtr handle,
  418. byte [] buffer,
  419. bool includeSubdirectories,
  420. NotifyFilters notifyFilter,
  421. out NativeOverlapped overlap,
  422. OverlappedHandler overlappedCallback);
  423. */
  424. }
  425. }