I have an ASP.NET Project, where I want to use the FileSystemWatcher. To do that I created an class "Watcher":
public class Watcher
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
Console.WriteLine("FileWatcherStarted");
using (FileSystemWatcher watcher = new FileSystemWatcher())
{
watcher.Path = @"C:\\FilesToWatch";
watcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
watcher.Filter = "*.txt";
watcher.Changed += OnChanged;
watcher.Created += OnChanged;
watcher.Deleted += OnChanged;
watcher.EnableRaisingEvents = true;
}
}
private static void OnChanged(object source, FileSystemEventArgs e) =>
Console.WriteLine($"{e.FullPath}, {e.ChangeType}");
}
Then I call this Run method in the IActionResult Index() method in the home controller with:
Watcher.Run();
Now when I want to run or debug my project i get no result from my file watcher. No error occurs. Theres just the message "Thread 0x123 ended with Code 0 (0x0)". Anyone, who had the same problem or knows the solution?