FileSystemWatcher changed event
I'm trying to monitor for new lines (represent orders) in any files in a directory using FSW. Everything works fine except when a new file is created in the folder. When it is created I get two Changed events. If the file exist I only get one. The problem is that the lines of text represent orders that are being posted to a database so the first order in the file gets posted twice. I can figure out a cluge to get it to work but doing it the right way would be better. Thanks for any help.
Wyatt
Here is the code:
fileSystemWatcher1.Filter = ConfigurationSettings.AppSettings.Get("DirectoryMonitorFilter");
fileSystemWatcher1.Path = ConfigurationSettings.AppSettings.Get("DirectoryMonitorPath");
fileSystemWatcher1.IncludeSubdirectories = (Convert.ToBoolean(ConfigurationSettings.AppSettings.Get("DirectoryMonitorIncludeSubs")));
fileSystemWatcher1.NotifyFilter = NotifyFilters.Size;
fileSystemWatcher1.Changed +=new System.IO.FileSystemEventHandler(fileSystemWatcher1_Changed);
fileSystemWatcher1.EnableRaisingEvents =true;
Do you have Anti-Virus software installed that may cause the Changed event to be raised unexpectedly?
If so, try disabling it and see if it works correctly.
Hello,
You get 2 "Changed" events, because 2 events happen (creation and modification). To tell them apart, you can use the ChangeType property of the FileSystemEventArgs instance that is provided as an argument to the Changed event handler:
void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{
if (e.ChangeType==WatcherChangeType.Changed) Do_Smth();
}
Hope this helps.
You're a lucky guy. I get two events on change too.. I only listen to LastWrite using the NotifyFilter property. And i check the ChangeType property of the event argument.
The changes are written with if not Notepad something very similar. But since I'm only listening til LasteWrite this should not matter (ref. problem with Notepad writing in batches). The files that are changed are only a few hundred bytes so the files should be written in one piece, I guess.
Here's my code:
_fsw = new FileSystemWatcher(watchPath);
_fsw.NotifyFilter = NotifyFilters.LastWrite;
_fsw.Changed +=new FileSystemEventHandler(MemoChanged);
private void MemoChanged(object sender, FileSystemEventArgs e)
{
if(e.ChangeType == WatcherChangeTypes.Changed)
{
//do stuff
}
}
HI,
I am creating a console app with VB.Net to watch and send a single notification whenever a file is created or overwritten in a directory.
But the event is raised 3 times more and hence the message is appearing more than the expected.
Please suggest how to avert it, is possible.
Thanks and Regards,
//Samik.
HI,
I am creating a console app with VB.Net to watch and send a single notification whenever a file is created or overwritten in a directory.
But the event is raised 3 times more and hence the message is appearing more than the expected.
Please suggest how to avert it, is possible.
Thanks and Regards,
//Samik.
Code snippet:
_FolderWatcher =
New System.IO.FileSystemWatcher
...
_FolderWatcher.NotifyFilter = IO.NotifyFilters.DirectoryName Or IO.NotifyFilters.FileName Or IO.NotifyFilters.LastWrite
AddHandler
_FolderWatcher.Created, AddressOf FldrActivity 'this handler will raise the event once AddHandler _FolderWatcher.Changed, AddressOf FldrActivity
.... Private
Sub FldrActivity(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs) Dim res As String If e.ChangeType = IO.WatcherChangeTypes.Changed Then res = "File " & e.FullPath & " has been modified" & vbCrLf
End If If e.ChangeType = IO.WatcherChangeTypes.Created Then res = "File " & e.FullPath & " has been created" & vbCrLf
End If _LogFile.WriteLine(res)
txtFSWOutput.Text &= res
End Sub