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;

[1127 byte] By [wyattblake] at [2008-2-24]
# 1

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.

DavidM.Kean at 2007-9-9 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 2
No change. The event is still fired twice. I've rigged it so that it doesn't process the event if it is within 5 seconds of the last time it was raised. This works for what I'm doing but I'd just like to know the correct way to do it or this is how it is designed to work.
wyattblake at 2007-9-9 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 3
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.

FlorinSab?u at 2007-9-9 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 4

Thanks for the input. I do check for ChangeType.Changed and it gets raised twice. The only thing that I can think of is that when the size goes from null to 0 KB it gets raised then from 0 KB to 1 KB it gets raised again.

wyattblake at 2007-9-9 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 5
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
}
}

TrondSmaavik at 2007-9-9 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 6
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.

SamikDutta at 2007-9-9 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 7
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

SamikDutta at 2007-9-9 > top of Msdn Tech,.NET Development,Common Language Runtime...

.NET Development

Site Classified