forked from topnguyen/Elect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileWatcher.cs
147 lines (131 loc) · 4.97 KB
/
FileWatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#region License
//--------------------------------------------------
// <License>
// <Copyright> 2018 © Top Nguyen </Copyright>
// <Url> http://topnguyen.com/ </Url>
// <Author> Top </Author>
// <Project> Elect </Project>
// <File>
// <Name> FileWatcher.cs </Name>
// <Created> 02/04/2018 8:21:00 PM </Created>
// <Key> 2a7a3ef5-2d71-4bea-b032-95dbddb17c46 </Key>
// </File>
// <Summary>
// FileWatcher.cs is a part of Elect
// </Summary>
// <License>
//--------------------------------------------------
#endregion License
using System.IO;
namespace Elect.Data.IO.FileUtils
{
public class FileWatcher
{
private readonly FileSystemWatcher _watcher;
/// <summary>
/// Create new instance of file system watcher
/// </summary>
/// <exception cref="System.ArgumentException">
/// The <paramref name="path" /> parameter contains invalid characters, is empty, or
/// contains only white spaces.
/// </exception>
/// <exception cref="PathTooLongException">
/// NoteIn the .NET for Windows Store apps or the Portable Class Library, catch the base
/// class exception, <see cref="T:System.IO.IOException" />, instead.The
/// <paramref name="path" /> parameter is longer than the system-defined maximum length.
/// </exception>
public FileWatcher(string path)
{
_watcher = new FileSystemWatcher
{
Path = Path.GetDirectoryName(path),
NotifyFilter = NotifyFilters.LastWrite,
Filter = Path.GetFileName(path),
IncludeSubdirectories = false
};
// Add Event
_watcher.Changed += OnFileChanged;
}
public FileWatcher(string path, OnChangedEvent onChangedEvent)
{
_watcher = new FileSystemWatcher
{
Path = Path.GetDirectoryName(path),
NotifyFilter = NotifyFilters.LastWrite,
Filter = Path.GetFileName(path),
IncludeSubdirectories = false
};
// Add Event
OnChanged = onChangedEvent;
_watcher.Changed += OnFileChanged;
}
/// <summary>
/// Delegate for event on change
/// </summary>
/// <param name="source"></param>
/// <param name="e"> </param>
public delegate void OnChangedEvent(object source, FileSystemEventArgs e);
/// <summary>
/// On file changed event
/// </summary>
public OnChangedEvent OnChanged { get; set; }
/// <summary>
/// Start watcher
/// </summary>
/// <exception cref="FileNotFoundException">
/// The directory specified in <see cref="P:System.IO.FileSystemWatcher.Path" /> could
/// not be found.
/// </exception>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="T:System.IO.FileSystemWatcher" /> object has been disposed.
/// </exception>
/// <exception cref="System.PlatformNotSupportedException">
/// The current operating system is not Microsoft Windows NT or later.
/// </exception>
/// <exception cref="System.ArgumentException">
/// <see cref="P:System.IO.FileSystemWatcher.Path" /> has not been set or is invalid.
/// </exception>
public void Start()
{
_watcher.EnableRaisingEvents = true;
}
/// <summary>
/// Stop watcher
/// </summary>
/// <exception cref="System.ObjectDisposedException">
/// The <see cref="T:System.IO.FileSystemWatcher" /> object has been disposed.
/// </exception>
/// <exception cref="FileNotFoundException">
/// The directory specified in <see cref="P:System.IO.FileSystemWatcher.Path" /> could
/// not be found.
/// </exception>
/// <exception cref="System.PlatformNotSupportedException">
/// The current operating system is not Microsoft Windows NT or later.
/// </exception>
/// <exception cref="System.ArgumentException">
/// <see cref="P:System.IO.FileSystemWatcher.Path" /> has not been set or is invalid.
/// </exception>
public void Stop()
{
_watcher.EnableRaisingEvents = false;
}
/// <summary>
/// On file changed event
/// </summary>
/// <param name="source"></param>
/// <param name="e"> </param>
private void OnFileChanged(object source, FileSystemEventArgs e)
{
try
{
_watcher.EnableRaisingEvents = false;
// do delegate
OnChanged?.Invoke(source, e);
}
finally
{
_watcher.EnableRaisingEvents = true;
}
}
}
}