12

I need to intercept when the system tries to access to a file, and do something before it happens.

Fionnuala
  • 90,370
  • 7
  • 114
  • 152
FBSC
  • 191
  • 2
  • 9

5 Answers5

8

You can use FileSystemWatcher - but this doesn't let you intercept the event and do things before hand - it only informs you after the fact that a particular file or directory has been affected in some way.

Intercepting file access is not something you can do easily.

You can write a file system filter driver, which is an unmanaged DLL which the O/S will load that can intercept operations on files or folders. However, this isn't a trivial task.

What are you trying to achieve, perhaps there's a simpler alternative?

LBushkin
  • 129,300
  • 32
  • 216
  • 265
2

Using just .net it is impossible

Yevhen
  • 1,897
  • 2
  • 14
  • 25
2

Check http://easyhook.codeplex.com/, it provides you directly from c# c++ like dll method overwritting.

Kerem Kusmezer
  • 492
  • 7
  • 15
1

There's a very similar thread here: How could I prevent a folder from being created using a windows service?. It may provide some more insight, but @LBushkin has already provided the same conclusion.

Community
  • 1
  • 1
Rune
  • 8,340
  • 3
  • 34
  • 47
0

Interesting question. What kind of access? Read, write, delete, copy?

  • If you need to prevent access to a file then the security would be the way. Then you can capture the access and do delegate the task to a higher privileged execution engine. Such as a service running as higher privileged user.

  • If you want to stop system level access then you'll need an IFS filter.

  • If all access is via your code, then I'd suggest a library that prevent's access.

  • And as other people have mentioned, there is the FileSystemWatcher, but I think this post access not before.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216