File Watching

AlaaMSH

New member
Joined
Aug 3, 2006
Messages
2
Programming Experience
10+
I have a ready-made application reading text data from a scanner and saves the data written to a text file. I'm trying to read the data written to that text file on the fly. I tried the FolderWatcher, but a problem arises. The problem simply is that windows doesn't allow file access by more than one process. So, the file is already open by the application, if I try to open the file after I'm notified that a change has been made to it, windows won't allow that, and access to file will be denied. Could I find please anyone with some information about that?
 
Last edited by a moderator:
Windows allows multiple processes to access a file, but only under certain conditions. File locks are granted toa process if no other lock barring them exists, and then that lock bars all others.

For example, if winamp is playing an MP3, it doesnt lock it for read, but it's impossible to write to it. UltraEdit (my favourite text editor) doesnt ususally lock files at all, so they can be worked with even when they are open. MS Excel, on the other hand, locks files against write while they are open (similar to winamp). I cannot recall an example of an app that locks a file against read but will allow write access.

If youre trying to watch some process's writings to a file from a high level, and that process has the file locked against reading (example if it is writing) then you will not succeed. One alternate tactic might be to hook into the windows API repsonsible for writing data to a file.

As an example, have a look at Filemon and it's peer apps developed by Bryce Cogswell and Mark Russinovich at www.sysinternals.com
A freeware app that shows file activity.. i seem to recall they do all kinds of apps that sniff data as it passes through the windows api routines that shunt it from A to B :)
 
Dear Cjard:
May be it was my fault I didn't write that bit of information in my post last night, but it's true, too many process can access one file at the same time, but this works for reading only, right? I don't have a problem with that, cuz I can open the file and read the line of text last written to it, but this works only for the first time ...
The ready-made application, reads data from the scanner and writes them to the text file periodically, i.e. every 10 seconds, now the problem that arises, it hangs and cannot write to that file if I'm opening that file with the program I'm writing. This is the problem.
Now, what I exactly want is to find out how can I know that data to be written to that file before they are written to the file, since the application will no longer be able to open the file if it's already open by another process. I guess this is the only way, I don't know if there might be another way to do it ...
Thank you a lot dear for your nice reply.
 
Back
Top