FileSystemWatcher.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. //
  11. using System;
  12. using System.ComponentModel;
  13. using System.Threading;
  14. namespace System.IO {
  15. [DefaultEvent("Changed")]
  16. public class FileSystemWatcher : Component, ISupportInitialize {
  17. #region Fields
  18. bool enableRaisingEvents;
  19. string filter;
  20. bool includeSubdirectories;
  21. int internalBufferSize;
  22. NotifyFilters notifyFilter;
  23. string path;
  24. ISite site;
  25. ISynchronizeInvoke synchronizingObject;
  26. #endregion // Fields
  27. #region Constructors
  28. public FileSystemWatcher ()
  29. {
  30. this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  31. this.enableRaisingEvents = false;
  32. this.filter = "*.*";
  33. this.includeSubdirectories = false;
  34. this.internalBufferSize = 8192;
  35. this.path = "";
  36. }
  37. public FileSystemWatcher (string path)
  38. : this (path, String.Empty)
  39. {
  40. }
  41. public FileSystemWatcher (string path, string filter)
  42. {
  43. if (path == null)
  44. throw new ArgumentNullException ("path");
  45. if (filter == null)
  46. throw new ArgumentNullException ("filter");
  47. if (path == String.Empty)
  48. throw new ArgumentException ("Empty path", "path");
  49. if (!Directory.Exists (path))
  50. throw new ArgumentException ("Directory does not exists", "path");
  51. this.enableRaisingEvents = false;
  52. this.filter = filter;
  53. this.includeSubdirectories = false;
  54. this.internalBufferSize = 8192;
  55. this.notifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  56. this.path = path;
  57. this.synchronizingObject = null;
  58. }
  59. #endregion // Constructors
  60. #region Properties
  61. [DefaultValue(false)]
  62. [IODescription("Flag to indicate if this instance is active")]
  63. public bool EnableRaisingEvents {
  64. get { return enableRaisingEvents; }
  65. set { enableRaisingEvents = value; }
  66. }
  67. [DefaultValue("*.*")]
  68. [IODescription("File name filter pattern")]
  69. [RecommendedAsConfigurable(true)]
  70. public string Filter {
  71. get { return filter; }
  72. set {
  73. filter = value;
  74. if (filter == null || filter == "")
  75. filter = "*.*";
  76. }
  77. }
  78. [DefaultValue(false)]
  79. [IODescription("Flag to indicate we want to watch subdirectories")]
  80. public bool IncludeSubdirectories {
  81. get { return includeSubdirectories; }
  82. set { includeSubdirectories = value; }
  83. }
  84. [Browsable(false)]
  85. [DefaultValue(8192)]
  86. public int InternalBufferSize {
  87. get { return internalBufferSize; }
  88. set { internalBufferSize = value; }
  89. }
  90. [DefaultValue(NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite)]
  91. [IODescription("Flag to indicate which change event we want to monitor")]
  92. public NotifyFilters NotifyFilter {
  93. get { return notifyFilter; }
  94. [MonoTODO ("Perform validation.")]
  95. set { notifyFilter = value; }
  96. }
  97. [DefaultValue("")]
  98. [IODescription("The directory to monitor")]
  99. [RecommendedAsConfigurable(true)]
  100. public string Path {
  101. get { return path; }
  102. set {
  103. bool exists = false;
  104. Exception exc = null;
  105. try {
  106. exists = Directory.Exists (value);
  107. } catch (Exception e) {
  108. exists = false;
  109. exc = e;
  110. }
  111. if (exc != null)
  112. throw new ArgumentException ("Invalid directory name", "value", exc);
  113. if (!exists)
  114. throw new ArgumentException ("Directory does not exists", "value");
  115. path = value;
  116. }
  117. }
  118. [Browsable(false)]
  119. public override ISite Site {
  120. get { return site; }
  121. set { site = value; }
  122. }
  123. [DefaultValue(null)]
  124. [IODescription("The object used to marshal the event handler calls resulting from a directory change")]
  125. public ISynchronizeInvoke SynchronizingObject {
  126. get { return synchronizingObject; }
  127. set { synchronizingObject = value; }
  128. }
  129. #endregion // Properties
  130. #region Methods
  131. [MonoTODO]
  132. public void BeginInit ()
  133. {
  134. throw new NotImplementedException ();
  135. }
  136. [MonoTODO]
  137. protected override void Dispose (bool disposing)
  138. {
  139. if (disposing) {
  140. //
  141. }
  142. base.Dispose (disposing);
  143. }
  144. [MonoTODO]
  145. public void EndInit ()
  146. {
  147. throw new NotImplementedException ();
  148. }
  149. private void RaiseEvent (Delegate ev, EventArgs arg)
  150. {
  151. if (ev == null)
  152. return;
  153. object [] args = new object [] {this, arg};
  154. if (synchronizingObject == null) {
  155. ev.DynamicInvoke (args);
  156. return;
  157. }
  158. synchronizingObject.BeginInvoke (ev, args);
  159. }
  160. protected void OnChanged (FileSystemEventArgs e)
  161. {
  162. RaiseEvent (Changed, e);
  163. }
  164. protected void OnCreated (FileSystemEventArgs e)
  165. {
  166. RaiseEvent (Created, e);
  167. }
  168. protected void OnDeleted (FileSystemEventArgs e)
  169. {
  170. RaiseEvent (Deleted, e);
  171. }
  172. protected void OnError (ErrorEventArgs e)
  173. {
  174. RaiseEvent (Error, e);
  175. }
  176. protected void OnRenamed (RenamedEventArgs e)
  177. {
  178. RaiseEvent (Renamed, e);
  179. }
  180. public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType)
  181. {
  182. return WaitForChanged (changeType, Timeout.Infinite);
  183. }
  184. [MonoTODO]
  185. public WaitForChangedResult WaitForChanged (WatcherChangeTypes changeType, int timeout)
  186. {
  187. throw new NotImplementedException ();
  188. }
  189. #endregion // Methods
  190. #region Events and Delegates
  191. [IODescription("Occurs when a file/directory change matches the filter")]
  192. public event FileSystemEventHandler Changed;
  193. [IODescription("Occurs when a file/directory creation matches the filter")]
  194. public event FileSystemEventHandler Created;
  195. [IODescription("Occurs when a file/directory deletion matches the filter")]
  196. public event FileSystemEventHandler Deleted;
  197. [Browsable(false)]
  198. public event ErrorEventHandler Error;
  199. [IODescription("Occurs when a file/directory rename matches the filter")]
  200. public event RenamedEventHandler Renamed;
  201. #endregion // Events and Delegates
  202. }
  203. }