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..
            Asked
            
        
        
            Active
            
        
            Viewed 390 times
        
    1 Answers
0
            
            
        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 Gan
        
- 2,813
- 2
- 29
- 38
- 
                    I already tried that, the app does not change the last access time. So it cannot be detected in that way. – blez Jun 03 '10 at 15:09
- 
                    1Maybe this answer to check if a file is open will help: http://stackoverflow.com/questions/2987559/check-if-the-file-is-open – Phil Gan Jun 07 '10 at 08:11
- 
                    Opening the file does not change the last access time. – tofutim Feb 27 '13 at 20:30
- 
                    Opening the file does not change the last access time - this is disabled in Vista onwards (though it can be re-enabled). http://www.groovypost.com/howto/microsoft/enable-last-access-time-stamp-to-files-folder-windows-7/ – tofutim Feb 27 '13 at 20:38
