galkin
asked on
ReadDirectoryChangesW
My application watches for change in specific file using ReadDirectoryChangesW Windows NT API function. Changes include file creation and last write. I user OVERLAPPED structure woth non signalled event. The structure is passed to ReadDirectoryChangesW function which immediately returns. Then I wait on event using WaitForSingleObject. When change in waching directory occurs ReadDirectoryChangesW signals event and WaitForSingleObject exits blocking state. Then I reset event and restart watch.
My problem is second time after I reset event ReadDirectoryChangesW immediately returns signaling event so I get two changes in the same file with the same parameters despite only one actual chnage (i.e. file was added to the directory) occured.
Could some one explain to me why ReadDirectoryChangesW notifies about file change twice?
My problem is second time after I reset event ReadDirectoryChangesW immediately returns signaling event so I get two changes in the same file with the same parameters despite only one actual chnage (i.e. file was added to the directory) occured.
Could some one explain to me why ReadDirectoryChangesW notifies about file change twice?
I assume that the first notification is due to the 'file was added to directory' incident, whilst the second notification is related to the 'last time modified changed' incident for the directory itself...
ASKER
Maybe, but I am actually interested in getting one notification, namely last write to file if it exists or creating file if it does not. Action filed of FILE_NOTIFY_INFORMATION structure has FILE_ACTION_MOFIFIED value in both cases. I need somehow to distinguish and omit case when time stamp chnaged.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here are the flags for that api...
FILE_NOTIFY_CHANGE_FILE_NA ME | // filter conditions to watch for
FILE_NOTIFY_CHANGE_DIR_NAM E |
FILE_NOTIFY_CHANGE_ATTRIBU TES |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_LAST_WR ITE |
FILE_NOTIFY_CHANGE_LAST_AC CESS |
FILE_NOTIFY_CHANGE_CREATIO N |
FILE_NOTIFY_CHANGE_SECURIT Y,
If you want to get file creation - specific notification, you can call readdirectorychanges multiple times, once for the notification of creation and once for the others, and use the event handle in WaitForMultipleObjects().. . The event that triggers is the one that will have the notification you want. E-mail your readdirectorychanges snippet to me and I can code what I'm talking about...
FILE_NOTIFY_CHANGE_FILE_NA
FILE_NOTIFY_CHANGE_DIR_NAM
FILE_NOTIFY_CHANGE_ATTRIBU
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_LAST_WR
FILE_NOTIFY_CHANGE_LAST_AC
FILE_NOTIFY_CHANGE_CREATIO
FILE_NOTIFY_CHANGE_SECURIT
If you want to get file creation - specific notification, you can call readdirectorychanges multiple times, once for the notification of creation and once for the others, and use the event handle in WaitForMultipleObjects()..
ASKER
Thank's for explanation. But i need to folter only one call. My another problem with ReadDirectoryChangesW is that it locks directory. I try from Explorer to duplicate file using copy/paste in directory wached but I have an error "Sharing valuation. Source or target may be in use"
ASKER
The problem is ReadDirectoryChangesW with FILE_NOTIFY_CHANGE_LAST_WR ITE notifies twice about change and there is no way do distinguish between chnage for instance in last write and time stampt as they are reffered in the same FILE_ACTION_MOFIFIED bit.