Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone, I am creating a small utility which will provide me the notifications based on the file changes. I wish to monitor the directory on following two conditions:

1. User copies and pastes a file to the directory.
2. User creates a new file in the directory.
3. User modifies the file in the directory.

I am able to achieve the points 1 and 2 with the following code:

C#
private void initializeWatcher(string path, string filter = "*.pdf")
{
    //If watcher is null, we create a new instance.
    watcher = watcher ?? new FileSystemWatcher();
    watcher.Filter = filter;
    watcher.Path = path;
    watcher.NotifyFilter = NotifyFilters.FileName;
    watcher.Changed += OnChanged;
    watcher.Created += OnChanged;
    watcher.Deleted += OnChanged;
}


I think that I need to modify the watcher.NotifyFilter property to achieve it. Can you please guide me how to achieve the third one?
Posted

1 solution

Didn't have the time to read the MSDN documents?

Excerpt from MDSN:
C#
/* Watch for changes in LastAccess and LastWrite times, and
   the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
   | NotifyFilters.FileName | NotifyFilters.DirectoryName;


Read all about it here: FileSystemWatcher.NotifyFilter Property[^].

Regards,

— Manfred
 
Share this answer
 
Comments
Pankaj Nikam 8-Apr-13 8:20am    
I read the documentation for the notify filter over here http://msdn.microsoft.com/en-us/library/system.io.notifyfilters.aspx
However I could not find anything for copy and paste. The problem is that if I set the property to LastWrite, I get the event fired twice.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900