MFC, observing a file on harddisk

I wonder if the following is feasible in MFC or WIN32.

I would like to read a file on hard disk, which I can do. Call that file fish.dat The catch is, I'd like to read it when and only when the file is changed, by a user or by another application.

Is that feasible? I was thinking that maybe Windows sends a message or notification of some sort whenever fish.dat is changed, and my program can in turn catch the message or notification which can cause it to read the file again. In this sense, my program is "observing" the file fish.dat. How can I do this in MFC? If Windows doesn't send a notification when fish.dat is changed, then how can I do something which will have the same effect (of observing the file with my program)??

I know this might be tricky to answer but I really appreciate your help. Thank you in advance.
[875 byte] By [Fatboy] at [2007-11-20 9:54:01]
# 1 Re: MFC, observing a file on harddisk
I wonder if the following is feasible in MFC or WIN32.

I would like to read a file on hard disk, which I can do. Call that file fish.dat The catch is, I'd like to read it when and only when the file is changed, by a user or by another application.

Is that feasible? I was thinking that maybe Windows sends a message or notification of some sort whenever fish.dat is changed, and my program can in turn catch the message or notification which can cause it to read the file again. In this sense, my program is "observing" the file fish.dat. How can I do this in MFC? If Windows doesn't send a notification when fish.dat is changed, then how can I do something which will have the same effect (of observing the file with my program)??

I know this might be tricky to answer but I really appreciate your help. Thank you in advance.

You would have to write either a 'proxy' file system driver, that would call the real driver, or... You would have to install 'proprietary hooks' that involves alot of work. Basically, a system wide hook that intercepts WriteFile, etc... calls to monitor before passing to WriteFile etc...
JamesSchumacher at 2007-11-9 13:31:44 >
# 2 Re: MFC, observing a file on harddisk
On win32 from Windows 2000 and up (not 98 or older), where the file system is NTFS, you can use the journaled file system features.

Basically, the idea is this: For NTFS (or any journaled file system), the operating system has a 'log' of changes made to the file system. You can request a callback on various changes, and assign them to directories (and files, I think). That is, if a file is altered, removed, created, etc. you get a callback indicating what that is.

In order to improve performance, change notices are sent only on the first change in a series, but not for subsequent changes, for a configured duration. That is, if a process is writing to a file, and continuing to do so, you'll get notification of the first update, not the others, until some time has passed.

While that generally doesn't matter much, it does mean that you might act upon a change notice while the file is still being modified, so some deductive reasoning is required.

Search MSD Platform SDK for Change Journal Records and research from there.

You might also find material by a search for Directory Watcher. I think there's also an article on the CodeProject website with a few classes that implement this.
JVene at 2007-11-9 13:32:43 >
# 3 Re: MFC, observing a file on harddisk
I think this is what JVene thinks of http://www.codeproject.com/file/DirCheck.asp
S_M_A at 2007-11-9 13:33:41 >
# 4 Re: MFC, observing a file on harddisk
Thank you both, that was very helpful. However, the article says something about a class called CNotifyDirCheck. I can't find any documentation on it in .NET reference, and I don't think it's in afxwin.h. Do you have any idea where I can find this class? Either documentation or source code??
Fatboy at 2007-11-9 13:34:45 >
# 5 Re: MFC, observing a file on harddisk
Did you download the source files? (can be found at top of page)
S_M_A at 2007-11-9 13:35:43 >
# 6 Re: MFC, observing a file on harddisk
Oh yeah, I was being stupid.

It doesn't look like I need his classes anyway. The functions in the SDK look sufficient for what I want to do with my program. Thank you sooooo much, that was very helpful!
Fatboy at 2007-11-9 13:36:41 >