Skip to content

Commit 248639b

Browse files
committed
Fixed crash when a file appears and is quickly deleted in a watched folder.
1 parent 27f8687 commit 248639b

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

VidCoderFileWatcher/Services/SystemFolderWatcher.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,31 @@ private async void OnFileDeleted(object sender, FileSystemEventArgs e)
9393

9494
private void HandleAndFilterNewFileOrFolder(string path)
9595
{
96-
if (CommonFileUtilities.IsDirectory(path))
96+
try
9797
{
98-
this.logger.Log("Path is a folder. Searching for video sources inside.");
99-
List<SourcePathWithType> paths = this.watcherService.GetPathsInWatchedFolder(path, this.watchedFolder);
100-
foreach (var sourcePath in paths)
98+
if (CommonFileUtilities.IsDirectory(path))
99+
{
100+
this.logger.Log("Path is a folder. Searching for video sources inside.");
101+
List<SourcePathWithType> paths = this.watcherService.GetPathsInWatchedFolder(path, this.watchedFolder);
102+
foreach (var sourcePath in paths)
103+
{
104+
this.logger.Log("Found new source: " + sourcePath.Path);
105+
this.HandleAndFilterNewFile(sourcePath.Path);
106+
}
107+
}
108+
else
101109
{
102-
this.logger.Log("Found new source: " + sourcePath.Path);
103-
this.HandleAndFilterNewFile(sourcePath.Path);
110+
this.HandleAndFilterNewFile(path);
104111
}
105112
}
106-
else
113+
catch (FileNotFoundException)
114+
{
115+
// File was deleted before we could handle it, just skip it
116+
}
117+
catch (Exception exception)
107118
{
108-
this.HandleAndFilterNewFile(path);
119+
// The file may be deleted or inaccessible, log and skip
120+
this.logger.Log($"Error occurred while handling new file/folder {path}:{Environment.NewLine}{exception}");
109121
}
110122
}
111123

0 commit comments

Comments
 (0)