Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DirectoryWatcher.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using System.Threading;
6 
7 namespace SiliconStudio.Core.IO
8 {
9  /// <summary>
10  /// Track file system events from several directories.
11  /// </summary>
12  public partial class DirectoryWatcher : IDisposable
13  {
14  private const int SleepBetweenWatcherCheck = 200;
15 #if !SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
16  private Thread watcherCheckThread;
17 #endif
18  private bool exitThread;
19 
20  /// <summary>
21  /// Occurs when a file/directory change occured.
22  /// </summary>
23  public event EventHandler<FileEvent> Modified;
24 
25  /// <summary>
26  /// Initializes a new instance of the <see cref="DirectoryWatcher"/> class.
27  /// </summary>
28  /// <param name="fileFilter">The file filter By default null default to *.*</param>
29  public DirectoryWatcher(string fileFilter = null)
30  {
31  FileFilter = fileFilter ?? "*.*";
32  InitializeInternal();
33  }
34 
35  /// <summary>
36  /// Gets the file filter used by this instance.
37  /// </summary>
38  /// <value>The file filter.</value>
39  public string FileFilter { get; private set; }
40 
41  /// <summary>
42  /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
43  /// </summary>
44  public virtual void Dispose()
45  {
46  exitThread = true;
47 #if !SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
48  if (watcherCheckThread != null)
49  {
50  watcherCheckThread.Join();
51  watcherCheckThread = null;
52  }
53 #endif
54  DisposeInternal();
55  }
56 
57  /// <summary>
58  /// Gets a list of current directories being tracked.
59  /// </summary>
60  /// <returns>A list of current directories being tracked</returns>
61  public List<string> GetTrackedDirectories()
62  {
63  return GetTrackedDirectoriesInternal();
64  }
65 
66  /// <summary>
67  /// Tracks the specified path.
68  /// </summary>
69  /// <param name="path">The path.</param>
70  /// <remarks>
71  /// If path is a file, this will used the parent directory. If the path is invalid, it will not fail but just skip it.
72  /// </remarks>
73  public void Track(string path)
74  {
75  TrackInternal(path);
76  }
77 
78  /// <summary>
79  /// UnTracks the specified path.
80  /// </summary>
81  /// <remarks>
82  /// If path is a file, this will used the parent directory. If the path is invalid, it will not fail but just skip it.
83  /// </remarks>
84  public void UnTrack(string path)
85  {
86  UnTrackInternal(path);
87  }
88 
89  /// <summary>
90  /// Called when a file event occured.
91  /// </summary>
92  /// <param name="sender">The sender.</param>
93  /// <param name="e">The file event.</param>
94  protected virtual void OnModified(object sender, FileEvent e)
95  {
96  var handler = Modified;
97  if (handler != null) handler(sender, e);
98  }
99 
100 #if !SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
101  // Doesn't throw any exceptions on other platforms
102 
103  private void InitializeInternal()
104  {
105  }
106 
107  private void DisposeInternal()
108  {
109  }
110 
111  private void TrackInternal(string path)
112  {
113  }
114 
115  private void UnTrackInternal(string path)
116  {
117  }
118 
119  private List<string> GetTrackedDirectoriesInternal()
120  {
121  return new List<string>();
122  }
123 #endif
124  }
125 }
EventHandler< FileEvent > Modified
Occurs when a file/directory change occured.
virtual void OnModified(object sender, FileEvent e)
Called when a file event occured.
DirectoryWatcher(string fileFilter=null)
Initializes a new instance of the DirectoryWatcher class.
void Track(string path)
Tracks the specified path.
virtual void Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resourc...
Track file system events from several directories.
List< string > GetTrackedDirectories()
Gets a list of current directories being tracked.
void UnTrack(string path)
UnTracks the specified path.
Ä file event used notified by DirectoryWatcher
Definition: FileEvent.cs:10