Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Excel
I am writing to excel file. I am raising a event whenever the excel file changes.
 
private void FileChanged()
       {
 
           watcher.Path = DirectoryPath;
           watcher.Filter = "*.xlsx";
           watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
 
           watcher.Changed += new FileSystemEventHandler(OnChanged);
           watcher.EnableRaisingEvents = true;
 
       }
 

I am expecting the OnChanged method would be called when I Open the file for writing but I am seeing the OnChanged method keeps getting called several times.
 
In the OnChanged method I am not doing anything if it is the same process. Only if it is a different process then I am reading from excel file.
 
Your help is appreciated.
Posted 4-Jan-13 10:14am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

MSDN[^] wrote:
Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.

What you will likely need to do is buffer the events coming out of the FileSystemWatcher.
 
Something like the following might work[^].
 
You might want to do some digging around for other implementations, as I'm not familiar with that but it seems like that could prove useful as a starting point.
 
Enjoy.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

JSOP also has two articles which (may) help you understand some of the things going on - see
 
FileSystemWatcher - Pure Chaos (Part 1 of 2)[^]
 
and part 2, particularly for the 'quirks'
 
FileSystemWatcher - Pure Chaos (Part 2 of 2)[^]
 

'G'
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Gihan Liyanage 332
1 Sergey Alexandrovich Kryukov 330
2 ClimerChinna 222
3 OriginalGriff 211
4 Richard MacCutchan 170
0 Sergey Alexandrovich Kryukov 8,298
1 OriginalGriff 7,112
2 CPallini 2,598
3 Richard MacCutchan 2,005
4 Abhinav S 1,788


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100