Search code examples
c#filesystemwatcher

get file contents on file open event in windows using c#


I am trying to get contents of file before it opens. I explored it on Internet but it didn't helped me more. Here is the code:

  FileSystemWatcher watcher = new FileSystemWatcher();
  NotifyFilters.FileName |
      NotifyFilters.DirectoryName | NotifyFilters.Attributes | NotifyFilters.CreationTime;
  watcher.Changed += new FileSystemEventHandler(OnChanged);
  watcher.Created += new FileSystemEventHandler(OnChanged);
  watcher.Deleted += new FileSystemEventHandler(OnChanged);
  watcher.Renamed += new RenamedEventHandler(OnRenamed);                       
  watcher.IncludeSubdirectories = true;
  watcher.EnableRaisingEvents = true;

But it is not giving me a way to get file open event. Please help me to get the solution.


Solution

  • If you want to get contents before it opens, it sounds like you need a hook for the filesystem. In this case you need to learn windows kernel programming (and that will require you to use C++ or some other unmananaged language)

    I can't think of an application that needs this other than antivirus/backup software.

    I guess that releasing an application that hooks to the kernel requires code signing by Microsoft or a trusted entity.