I have created a FileSystem watcher and have registered 2 events. One is for creation and other for change.
FileSystemWatcher fswInbound = new FileSystemWatcher();
fswInbound.Path = "path to directory";
fswInbound.EnableRaisingEvents = true;
fswInbound.InternalBufferSize = 12288;
fswInbound.Created += new FileSystemEventHandler(program.FileSystemWatcher_Created);
fswInbound.Changed += new FileSystemEventHandler(program.FileSystemWatcher_Created);
Than in same program I am creating files dynamically and trying to write into folder where FileSystem Watcher is listening.
using (StreamWriter outfile = new StreamWriter(filename))
{
outfile.Write("Some really long string");
}
After a new file is created. 2 events are getting fired. One is for change and other for creation. I only want the event of file creation to trigger. How can I avoid File Change event when I am creating file for the first time?
That is by design according to MSDN To work around it I would have a common event handler which it looks like you have, Then I would check the the FileSystemEventArgs to check the ChangeType for file Creation or Change.
From above link: