Can I use C to know whether a file operation is performed (and where it is performed) on the disk at run time?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The answer to your question depends on how you meant it. If you’re asking if you can detect a physical operation on the disk (seek, read, or write), the answer is no. Disks have disk caches to hide these operations from you in the name of performance. You can tell when the cache is changed using the mechanisms above, but not when the physical operation is performed.
This is why we have journaling filesystems. Even with journaling, however, there is the possibility of actual data loss if there is a catastrophic failure and the cache cannot be saved.
Of the above methods, I have used the FileSystemWatcher, and it works very well. You can select the events you’re interested in being notified about the your program will receive an program event when the file event takes place.
Note that, at least under Windows/C#, the event comes in on a separate thread, so you’ll have to use delegates to signal the main (or whatever) thread that the event has taken place.