Click here to Skip to main content
15,914,013 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to trace a file (ex C:\test.txt). When have an action on this file such as edit, modify, read, ... i want to write action to logfile. How to do that?
I think should be using Dokan Library. But i'm not sure about it. Please help me. Thanks!
Posted

You can use FileSystemWatcher class from System.IO namespace of .NET framework. Herez the code snippet:
C#
public class MyFileWatcher
{
    public static void Main()
    {
    Run();
    }

    [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
    public static void Run()
    {
        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = @"c:\test.txt";
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);

        // Begin watching.
        watcher.EnableRaisingEvents = true;

        while(Console.Read()!='q');
    }

    // Define the event handlers.
    private static void OnChanged(Object source,FileSystemEventArgs e)
    {
    .............
    }
 
Share this answer
 
Comments
mot sach 9-Oct-11 23:53pm    
Using FileSystemWatcher class is so easy to handle events on file. But, how to get process id which acting on this file (ex: get process id of notepad when using notepad to open "C:\Test.txt").
File operations can be monitored either using FileSystemWatcher in .NET or using file system filter driver (which you have confused with a filesystem driver). The only user-mode solution (based on kernel-mode driver) is CallbackFilter.
 
Share this answer
 
Comments
mot sach 10-Oct-11 0:03am    
thanks so much. If you have used Dokan lib before, please explain to me: "how to use that to trace a file"?
Eugene Mayeski 10-Oct-11 0:42am    
You have not read my reply. You must use filter driver, and Dokan is not a filter driver. You can't use Dokan.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900