Zaina
❔ FILewatcher raise multiple events when a file is created
Hello everyone. I have a code that alert me when a file is created in a folder (the file is created by an external process). the problem is that the event Handlecreated is raised multiple times for a single file. Does anyone know why ?
this is the code :
string extractionFolder = @"C:\Extractions";
FileSystemWatcher watcher = new FileSystemWatcher(extractionFolder);
watcher.Filter = "*.csv";
watcher.Created += HandleCreated;
watcher.Error += HandleError;
watcher.EnableRaisingEvents = true;
private async void HandleCreated(object sender, FileSystemEventArgs e)
{
int readAttempt = 0; int maxDuration = 15; while (!IsFileReady(e.FullPath) && readAttempt < maxDuration) { readAttempt++; System.Threading.Thread.Sleep(1000); } if (IsFileReady(e.FullPath)) {
await _hubContext.Clients.All.SendAsync("ReceiveMessage", e.FullPath); }
}
int readAttempt = 0; int maxDuration = 15; while (!IsFileReady(e.FullPath) && readAttempt < maxDuration) { readAttempt++; System.Threading.Thread.Sleep(1000); } if (IsFileReady(e.FullPath)) {
await _hubContext.Clients.All.SendAsync("ReceiveMessage", e.FullPath); }
}
9 replies