FileSystemWatcher

P

Peter Kirk

Hi

I am trying to use FileSystemWatcher to watch for changes in a directory.
The problem I am exepriencing is a double firing of an OnChanged event when
a file is opened, edited, and saved. Why is the event fired twice?

Here is my code:

static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"g:\watchme";
watcher.Filter = "";
//watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.Deleted += new FileSystemEventHandler(OnDeleted);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.IncludeSubdirectories = false;
watcher.EnableRaisingEvents = true;
Console.Read();
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("OnChanged: FullPath=" + e.FullPath + "; Name=" +
e.Name);
}
private static void OnCreated(object source, FileSystemEventArgs e)
{
Console.WriteLine("OnCreated: FullPath=" + e.FullPath + "; Name=" +
e.Name);
}
private static void OnDeleted(object source, FileSystemEventArgs e)
{
Console.WriteLine("OnDeleted: FullPath=" + e.FullPath + "; Name=" +
e.Name);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("OnRenamed: FullPath=" + e.FullPath + "; Name=" + e.Name
+ "; OldFullPath=" + e.OldFullPath + "; OldName=" + e.OldName);
}

Thanks,
Peter
 
P

Peter Kirk

I open the file in notepad (it's a simple text file), add a character, and
save/close the file.

Note that in the code I presented I have not set "watcher.NotifyFilter" to
anything. It didn't actually seem to make a difference to the results I saw.

Peter

Moty Michaely said:
Hey Peter,

What operation are you trying to do exactly on the file that fires those
events?

If you are trying just to save, LastWrite notify will fire as much times
as the application saving the file writes and flushes the file.

- Moty -

Peter Kirk said:
Hi

I am trying to use FileSystemWatcher to watch for changes in a directory.
The problem I am exepriencing is a double firing of an OnChanged event
when a file is opened, edited, and saved. Why is the event fired twice?

Here is my code:

static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"g:\watchme";
watcher.Filter = "";
//watcher.NotifyFilter = NotifyFilters.FileName |
NotifyFilters.LastWrite;
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.Deleted += new FileSystemEventHandler(OnDeleted);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.IncludeSubdirectories = false;
watcher.EnableRaisingEvents = true;
Console.Read();
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("OnChanged: FullPath=" + e.FullPath + "; Name=" +
e.Name);
}
private static void OnCreated(object source, FileSystemEventArgs e)
{
Console.WriteLine("OnCreated: FullPath=" + e.FullPath + "; Name=" +
e.Name);
}
private static void OnDeleted(object source, FileSystemEventArgs e)
{
Console.WriteLine("OnDeleted: FullPath=" + e.FullPath + "; Name=" +
e.Name);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("OnRenamed: FullPath=" + e.FullPath + "; Name=" +
e.Name + "; OldFullPath=" + e.OldFullPath + "; OldName=" + e.OldName);
}

Thanks,
Peter
 
M

Moty Michaely

Hey Peter,

What operation are you trying to do exactly on the file that fires those
events?

If you are trying just to save, LastWrite notify will fire as much times as
the application saving the file writes and flushes the file.

- Moty -
 
M

Moty Michaely

I missed that point :)

Try to set the NotifyFilter to be NotifyFilters.LastWrite to watch for
changes.

If you want to watch for file rename events set the filters to be
NotifyFilters.FileName. etc.

I am here for any further comments.
-Moty-

Peter Kirk said:
I open the file in notepad (it's a simple text file), add a character, and
save/close the file.

Note that in the code I presented I have not set "watcher.NotifyFilter" to
anything. It didn't actually seem to make a difference to the results I
saw.

Peter

Moty Michaely said:
Hey Peter,

What operation are you trying to do exactly on the file that fires those
events?

If you are trying just to save, LastWrite notify will fire as much times
as the application saving the file writes and flushes the file.

- Moty -

Peter Kirk said:
Hi

I am trying to use FileSystemWatcher to watch for changes in a
directory.
The problem I am exepriencing is a double firing of an OnChanged event
when a file is opened, edited, and saved. Why is the event fired twice?

Here is my code:

static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"g:\watchme";
watcher.Filter = "";
//watcher.NotifyFilter = NotifyFilters.FileName |
NotifyFilters.LastWrite;
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.Deleted += new FileSystemEventHandler(OnDeleted);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.IncludeSubdirectories = false;
watcher.EnableRaisingEvents = true;
Console.Read();
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("OnChanged: FullPath=" + e.FullPath + "; Name=" +
e.Name);
}
private static void OnCreated(object source, FileSystemEventArgs e)
{
Console.WriteLine("OnCreated: FullPath=" + e.FullPath + "; Name=" +
e.Name);
}
private static void OnDeleted(object source, FileSystemEventArgs e)
{
Console.WriteLine("OnDeleted: FullPath=" + e.FullPath + "; Name=" +
e.Name);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("OnRenamed: FullPath=" + e.FullPath + "; Name=" +
e.Name + "; OldFullPath=" + e.OldFullPath + "; OldName=" + e.OldName);
}

Thanks,
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top