I have external app reading files, I want to hook that to get event in my app. But I cannot find a sources hooking ReadFile (or something else that can help me achieve that). Any ideas how to do that? It must be done in User-Mode. I was thinking for something similar to Process Monitor. I wonder how it does it..
A:
In .net you can use the FileSystemWatcher. You will need to add a handler for the Changed event which will detect a change in the last access time (amongst other things).
From the MSDN example linked above:
public static void Foo()
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"Your path";
/* Watch for changes in LastAccess time */
watcher.NotifyFilter = NotifyFilters.LastAccess;
// Only watch text files.
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
Phil
2010-06-03 15:03:27
I already tried that, the app does not change the last access time. So it cannot be detected in that way.
blez
2010-06-03 15:09:33
Does it lock the read file?
Phil
2010-06-03 15:21:09
No, it doesn't.
blez
2010-06-03 15:22:51
Maybe this answer to check if a file is open will help: http://stackoverflow.com/questions/2987559/check-if-the-file-is-open
Phil
2010-06-07 08:11:43