/SparkleLib/SparkleWatcher.cs

http://github.com/hbons/SparkleShare · C# · 63 lines · 33 code · 15 blank · 15 comment · 0 complexity · bb2c6d1373f81564d729aa4b6200ba03 MD5 · raw file

  1. // SparkleShare, a collaboration and sharing tool.
  2. // Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as
  6. // published by the Free Software Foundation, either version 3 of the
  7. // License, or (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using System;
  17. using System.IO;
  18. namespace SparkleLib {
  19. public class SparkleWatcher : FileSystemWatcher {
  20. public event ChangeEventEventHandler ChangeEvent = delegate { };
  21. public delegate void ChangeEventEventHandler (FileSystemEventArgs args);
  22. private Object thread_lock = new Object ();
  23. public SparkleWatcher (string path) : base (path)
  24. {
  25. IncludeSubdirectories = true;
  26. EnableRaisingEvents = true;
  27. Filter = "*";
  28. Changed += OnChanged;
  29. Created += OnChanged;
  30. Deleted += OnChanged;
  31. Renamed += OnChanged;
  32. }
  33. private void OnChanged (object sender, FileSystemEventArgs args)
  34. {
  35. ChangeEvent (args);
  36. }
  37. public void Enable ()
  38. {
  39. lock (this.thread_lock)
  40. EnableRaisingEvents = true;
  41. }
  42. public void Disable ()
  43. {
  44. lock (this.thread_lock)
  45. EnableRaisingEvents = false;
  46. }
  47. }
  48. }