Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add the ability to filter file events by regular expression

  • Loading branch information...
commit 7ed098d8682e70f303160af76234c5e1524c66ad 1 parent 3ac9422
Taliesin Sisson authored
View
2  Talifun-FileWatcher.nuspec
@@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Talifun.FileWatcher</id>
- <version>1.0.0.0</version>
+ <version>1.0.1.0</version>
<title>Talifun.FileWatcher</title>
<authors>Taliesin Sisson</authors>
<owners>Talifun Ltd</owners>
View
2  build/settings.ps1
@@ -26,7 +26,7 @@ properties {
# BUILD_NUMBER is defined during CI builds. Make sure that this value
# is changed if the CI system in use does not set this variable.
# Make sure Semver versioning is used for the build number.
- $build.version = if($env:BUILD_NUMBER) { $env:BUILD_NUMBER } else { "1.0.0.0" }
+ $build.version = if($env:BUILD_NUMBER) { $env:BUILD_NUMBER } else { "1.0.1.0" }
$tools = @{}
$tools.dir = "$($base.dir)\tools"
View
80 src/Talifun.FileWatcher/EnhancedFileSystemWatcher.cs
@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
-using System.ComponentModel;
+using System.ComponentModel;
+using System.Text.RegularExpressions;
using System.Threading;
using System.Timers;
@@ -42,13 +43,15 @@ public EnhancedFileSystemWatcher(string folderToWatch, string filter, int pollTi
_fileSystemWatcherCreatedEvent = new FileSystemEventHandler(OnFileCreated);
_fileSystemWatcherDeletedEvent = new FileSystemEventHandler(OnFileDeleted);
_fileSystemWatcherRenamedEvent = new RenamedEventHandler(OnFileRenamed);
- _fileFinishedChangingCallback = new FileFinishedChangingCallback(OnFileFinishedChanging);
-
- _fileSystemWatcher = !string.IsNullOrEmpty(Filter) ? new FileSystemWatcher(FolderToWatch, Filter) : new FileSystemWatcher(FolderToWatch);
- _fileSystemWatcher.IncludeSubdirectories = IncludeSubdirectories;
- _fileSystemWatcher.EnableRaisingEvents = false;
- _fileSystemWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
- _fileSystemWatcher.Changed += _fileSystemWatcherChangedEvent;
+ _fileFinishedChangingCallback = new FileFinishedChangingCallback(OnFileFinishedChanging);
+
+ _fileSystemWatcher = new FileSystemWatcher(FolderToWatch)
+ {
+ IncludeSubdirectories = IncludeSubdirectories,
+ EnableRaisingEvents = false,
+ NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite
+ };
+ _fileSystemWatcher.Changed += _fileSystemWatcherChangedEvent;
_fileSystemWatcher.Created += _fileSystemWatcherCreatedEvent;
_fileSystemWatcher.Deleted += _fileSystemWatcherDeletedEvent;
_fileSystemWatcher.Renamed += _fileSystemWatcherRenamedEvent;
@@ -114,13 +117,15 @@ private void GetAllFilesToCheck(string folderPath)
}
}
- string[] files = null;
- files = !string.IsNullOrEmpty(Filter) ? Directory.GetFiles(folderPath, Filter) : Directory.GetFiles(folderPath);
+ string[] files = null;
+ files = Directory.GetFiles(folderPath);
foreach (var file in files)
- {
- var fileInfo = new FileInfo(file);
- Push(file, new FileSystemEventArgs(WatcherChangeTypes.All, fileInfo.DirectoryName, fileInfo.Name));
+ {
+ if (!ShouldMonitorFile(file)) continue;
+
+ var fileInfo = new FileInfo(file);
+ Push(file, new FileSystemEventArgs(WatcherChangeTypes.All, fileInfo.DirectoryName, fileInfo.Name));
}
}
@@ -146,7 +151,13 @@ private static bool IsFileLocked(string filePath)
return result;
}
- #region FilesChanging Queue
+ private bool ShouldMonitorFile(string fileName)
+ {
+ const RegexOptions regxOptions = RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Singleline;
+ return (string.IsNullOrEmpty(Filter) || Regex.IsMatch(fileName, Filter, regxOptions));
+ }
+
+ #region FilesChanging Queue
/// <summary>
/// A file event has occured on a file already waiting to be raised, so update the time
@@ -155,10 +166,13 @@ private static bool IsFileLocked(string filePath)
/// <param name="filePath"></param>
private void Touch(string filePath)
{
- if (string.IsNullOrEmpty(filePath)) return;
-
- var item = _filesChanging[filePath];
- item.FireTime = DateTime.Now.AddMilliseconds(PollTime);
+ if (string.IsNullOrEmpty(filePath)) return;
+
+ IFileChangingItem item;
+ if (_filesChanging.TryGetValue(filePath, out item))
+ {
+ item.FireTime = DateTime.Now.AddMilliseconds(PollTime);
+ }
if (_nextFileToCheck == filePath)
{
@@ -215,13 +229,12 @@ private void GetNextFileToCheck()
var nextFileToCheck = string.Empty;
foreach (var item in _filesChanging)
{
- var dateTime = item.Value.FireTime;
-
- if (currentDateTime < lowestDateTime)
- {
- lowestDateTime = dateTime;
- nextFileToCheck = item.Key;
- }
+ var dateTime = item.Value.FireTime;
+
+ if (currentDateTime >= lowestDateTime) continue;
+
+ lowestDateTime = dateTime;
+ nextFileToCheck = item.Key;
}
//There are no more files to raise events for
@@ -294,8 +307,9 @@ private void OnFileFinishedChanging(FileSystemEventArgs e)
private void OnFileCreated(object sender, FileSystemEventArgs e)
{
- var filePath = e.FullPath;
-
+ var filePath = e.FullPath;
+
+ if (!ShouldMonitorFile(filePath)) return;
if (e.ChangeType != WatcherChangeTypes.Created) return;
lock (_filesRaisingEventsLock)
{
@@ -315,7 +329,9 @@ private void OnFileCreated(object sender, FileSystemEventArgs e)
private void OnFileChanged(object sender, FileSystemEventArgs e)
{
- var filePath = e.FullPath;
+ var filePath = e.FullPath;
+
+ if (!ShouldMonitorFile(filePath)) return;
lock (_filesRaisingEventsLock)
{
@@ -335,7 +351,8 @@ private void OnFileChanged(object sender, FileSystemEventArgs e)
private void OnFileDeleted(object sender, FileSystemEventArgs e)
{
- var filePath = e.FullPath;
+ var filePath = e.FullPath;
+ if (!ShouldMonitorFile(filePath)) return;
lock (_filesRaisingEventsLock)
{
@@ -354,7 +371,8 @@ private void OnFileDeleted(object sender, FileSystemEventArgs e)
private void OnFileRenamed(object sender, RenamedEventArgs e)
{
- var filePath = e.FullPath;
+ var filePath = e.FullPath;
+ if (!ShouldMonitorFile(filePath)) return;
lock (_filesRaisingEventsLock)
{
@@ -373,8 +391,6 @@ private void OnFileRenamed(object sender, RenamedEventArgs e)
#endregion
-
-
#region FileChangedEvent
/// <summary>
/// Where the actual event is stored.
Please sign in to comment.
Something went wrong with that request. Please try again.