views:

179

answers:

3

Hi,

I am using this implementation of ReadDirectoryChangesW to monitor changes to the desktop. My program plans to run some small program when a file is created on the desktop.

Now the problem I am running into is when I create a new shortcut via the right click context menu ReadDirectoryChangesW gets a notification saying the file has been created, but right after it gets another notification saying the file has been deleted. I have been running into this problem since Windows Vista.

Anyone have any idea what could be wrong? Is there another function I should be using to monitor directory changes specific to Vista and 7?

Thanks, Krishna

A: 

Have you defined any filters: you might be seeing the shortcut being recreated (and not matching your filter).

Explorer tends to create things with a fixed name, and then rename them. So ReadDirectoryChangesW will see multiple changes.

Additional thoughts:

Note, the buffer with the change notification can contain multiple notifications, are all being read.

What does Process Monitor show at the same time?

Richard
Sorry I guess I was a bit unclear. I do see the shortcut get created, but after receiving a notification about the shortcut being created it is instantly followed with a notification that the file has been deleted which doesn't make sense.This implementation seems to use GetQueuedCompletionStatus to get the IO instruction that occurred, then return me a pointer to an object which will tell me which filters to apply for the ReadDirectoryChangesW call
I've expanded this. But in the end, there is no substitute for conventional debugging. It is very easy to use APIs like `ReadDirectoruChnagesW` poorly and mis-events. Do not presume Code Project code is correct.
Richard
A: 

Windows does some funny stuff with .lnk files so that you can't see the .lnk extension. Maybe that's why the deleted notification gets sent, because Windows itself can't see the "New Shortcut.lnk" file, only the "New Shortcut". But this is just a theory.

So what I would do is try to work around it, and ignore the file-deleted notification if the file still exists. Assuming you can figure out that the .lnk file does still exist of course :)

demoncodemonkey
A: 

I managed to resolve this issue. I still don't know why I am getting all those strange ReadDirectyChangesW events but I got my end result so I am leaving this question be.

Thanks for all the help.