Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept the access to a file in a .NET Program

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

like image 869
FBSC Avatar asked Feb 28 '10 21:02

FBSC


3 Answers

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?

like image 158
LBushkin Avatar answered Oct 16 '22 02:10

LBushkin


Using just .net it is impossible

like image 2
Yevhen Avatar answered Oct 16 '22 02:10

Yevhen


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

like image 2
Kerem Kusmezer Avatar answered Oct 16 '22 01:10

Kerem Kusmezer