Skip to content

Commit f6962da

Browse files
committed
Initial commit
0 parents  commit f6962da

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode
2+
bin
3+
obj

Program.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Microsoft.Diagnostics.Tracing.Parsers;
2+
using Microsoft.Diagnostics.Tracing.Session;
3+
4+
namespace WinFileReadEvents
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
if (TraceEventSession.IsElevated() != true)
11+
{
12+
Console.WriteLine("x|To turn on ETW events you need to be Administrator, please run from an Admin process.");
13+
return;
14+
}
15+
16+
string? filePath = (args.Length < 1) ? null : args[0];
17+
18+
using var session = new TraceEventSession("FileRead");
19+
20+
Console.CancelKeyPress += (sender, e) => session.Stop();
21+
22+
session.EnableKernelProvider(
23+
KernelTraceEventParser.Keywords.DiskFileIO |
24+
KernelTraceEventParser.Keywords.FileIOInit);
25+
26+
session.Source.Kernel.FileIORead += data =>
27+
{
28+
if (filePath == null || string.Compare(data.FileName, filePath, StringComparison.OrdinalIgnoreCase) == 0)
29+
{
30+
string line = ">|";
31+
line += data.Offset + "|";
32+
line += data.IoSize + "|";
33+
line += data.FileName;
34+
Console.WriteLine(line);
35+
}
36+
};
37+
38+
session.Source.Process();
39+
}
40+
}
41+
}

WinFileReadEvents.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>WinFileReadEvents</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="3.1.9" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)