Skip to content

Commit

Permalink
create watcher in the main method
Browse files Browse the repository at this point in the history
  • Loading branch information
mtokarev committed Sep 18, 2023
1 parent f95a96a commit 00a0870
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/FolderDog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ private static void Main(string[] args)
_messageSender = new EmailService(_emailOptions, _logger);
_fileService = new FileService(_fileServiceOptions, _logger);
_webhookService = new WebhookService(_webhookOptions, _logger);
ConfigureFileListener();
var watcher = new FileSystemWatcher(_bindingOptions.FolderPath);
ConfigureFileListener(watcher);

var fullPath = string.Equals(_bindingOptions.FolderPath, "./")
? Directory.GetCurrentDirectory()
Expand All @@ -54,22 +55,21 @@ private static void Main(string[] args)
/// <summary>
/// Set file listeners
/// </summary>
private static void ConfigureFileListener()
private static void ConfigureFileListener(FileSystemWatcher watcher)
{
foreach(string fileExtension in _bindingOptions.FileExtensions)
{
var watcher = new FileSystemWatcher(_bindingOptions.FolderPath);
watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.Size;

watcher.Error += OnError;
watcher.Filter = $"*.{fileExtension}";
watcher.IncludeSubdirectories = _bindingOptions.ListenInSubfolders;
watcher.EnableRaisingEvents = true;

// Registering handlers
watcher.Error += OnError;
if (!string.IsNullOrEmpty(_webhookOptions.Url))
{
watcher.Created += async (object sender, FileSystemEventArgs e)
Expand All @@ -79,8 +79,7 @@ private static void ConfigureFileListener()
if (!string.IsNullOrEmpty(_emailOptions.SmtpServerHost))
{
watcher.Created += OnCreatedMailSendAsync;
}

}
}
}

Expand Down

0 comments on commit 00a0870

Please sign in to comment.