boop
boop

Reputation: 7787

How to monitor file access?

I want to monitor when some process reads a specific file

When I use procmon to monitor the said file I can see that it gets queried and read a lot

Event Traces

Now I want to monitor a specific file and act once the CloseFile operation was executed on said file.

So I've installed the Microsoft.Diagnostics.Tracing.TraceEvent nuget package and tried numerous things to get this working. It seems to work randomly? But it hasn't worked in a while.

Couple of things to mention:

Here is my code. What am I doing wrong?

await Task.Run(() =>
{
    using TraceEventSession session = new("MySession");
    session.EnableKernelProvider(KernelTraceEventParser.Keywords.All);
    session.Source.Kernel.FileIOClose+= data =>
    {
        if (data.FileName == @"c:\foo.txt")
        {
            // do stuff
        }
    };
    session.Source.Process();
});

Upvotes: 1

Views: 82

Answers (0)

Related Questions