Windows Programming: A ReadDirectoryChangesW question

Hey technical folks,

I’m attempting to use ReadDirectoryChangesW to watch a directory for when a file is added to it. The problem is that I get the notification when the add begins, and not when its actually finished. Is there a way to only get that message when its finished?

Note: I’ve done some googling and I actually found a question almost identical to this here: http://www.experts-exchange.com/Programming/Q_21004316.html

But thats a pay subscription site and i’m a cheap stubborn and paranoid bastard, i figure once i subscribe there is no guarantee that the solution even exists or is useful.

PS if someone here knows the site and thinks its useful for programming in general, maybe i’ll just cave and buy a sub.

EDIT: One hacky solution so far is to just while loop whatever action i want to perform on the file until it succeeds, assuming that the reason why its failing is because the file isn’t done copying, but this just seems like bad form to me.

I beleive the flag you want is:
FILE_NOTIFY_CHANGE_LAST_WRITE

I am not sure, but I think that the write time of a file only changes after it has been closed, not during or at the start of a write operation.

The function details are in the MSDN Article.

You can also try change journals as long as you are running on win2k or later.

Another important thing is to open the directory with these flags:
FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE

If you don’t, then all files in that directory will be denied the missing access. For example, if you do not have the FILE_SHARE_DELETE you will not be able to delete any files in the watched directory.

Thanks for the reply DeepT. I tried your suggestion of using FILE_NOTIFY_CHANGE_LAST_WRITE, but I guess the write time of the file changes when the writing first occurs. It did lead me to try FILE_NOTIFY_CHANGE_LAST_ACCESS though, that seemed to solve my problem.

Yeah, MSDN was my first stop for info on the function, but links are always good. I may have to swap to use change journals, but that seems like alot of work for a small program, i may just

Look, I want you to understand that I’m not accusing you of being stupid, it’s just that in the past, I know for a fact that other people have been misled by expert sexchange’s page design. The answers to that question are actually on that page and not behind a $$$ barrier - you just have to scroll down past the ads.

Now it’s entirely possible that YOU KNOW THIS ALREADY, and those solutions are no good for you. I’m just saying this just in case.

FileSystemWatcher?

Actually, what experts-exchange seems to do is that they’ll show you the solution the first time you visit their site (from that IP address, I’d guess), but the next time you visit they’ll give you the “You must be subscribed to view this solution” prompt.

E.g., I visited the page from this machine and got the solution, hit refresh, and the solution was replaced by “You must subscribe…”

Edit: Oh, actually it seems to be cookie-based. Try clearing out any cookies you have for their domain. (And hope the secret DMCA police don’t drag you away to do COBOL maintenance.)

Don’t be so quick to accuse. What fugative said is true. I had a question at some time in the past and people were saying what you said to me.

I had to get screen shots of the web page to prove that they were not there for me.