2

Hello everybody and sorry if i duplicate question(my english so bad). Is there any way to be notified when files are opened by a user? Something like the FileSystemWatcher class, except with an "Opened" event? I just want detect opening file without changed or renamed this file. for example, now i detect renaming file and want detecting opening too

private void hookEvents(String path, String password)
    {
        FileSystemWatcher fsw = new FileSystemWatcher();
        fsw.Path = Path.GetDirectoryName(path);
        fsw.Filter = Path.GetFileName(path);
        fsw.Renamed += new RenamedEventHandler(onRenamed);
        fsw.EnableRaisingEvents = true;
    }

P.S. i hear some about filters and drivers, but i hope that c# have simple way for my request.

mechanikos
  • 771
  • 12
  • 32
  • The closest thing I think you could get to working with opened would be to key off the Modified Date, you would need to implement the NotifyFilter Property of the Watcher class. See here http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.filter(v=vs.110).aspx – Bearcat9425 Mar 18 '14 at 14:16

2 Answers2

2

System.IO.FileSystemWatcher is built for monitoring file changes. As "opening a file" does not change the file itself, it won't work for sure.

there are possible ways to do this

Option 1

You can detect if a file is in use either by a WMI Query (if your OS is windows based) or by opening the file for write (Exception occurs when file is in use).

Option 2

You could try to get a handle on the file, and see the which process has the ownership. If so, the file is open by somebody else. check this post Using C#, how does one figure out what process locked a file?

Community
  • 1
  • 1
Overmachine
  • 1,723
  • 3
  • 15
  • 27
  • hmm... i thing first way is possible for my brain. one question: when exception occurs? when file is open in some programm and i try open for write in my app, or if i first open file in my app and after try open(with notepad for examlpe) i catch exception too? – mechanikos Mar 18 '14 at 15:30
2

I don't think there is a simple answer that can be implemented in C#.

There is some info in this question but it's all fairly low level stuff... File system filter drivers and API hooks!

Do you want to monitor a small set of files in a folder, or the whole disk?

Community
  • 1
  • 1
Lee Willis
  • 1,552
  • 9
  • 14
  • i need simple monitor for specific file which will ask password when i trying open file(i read about acl and hope that i will not have any problems with deny accsess) – mechanikos Mar 18 '14 at 14:41
  • So if they enter the wrong password you want to stop the file opening? What is the file type? If it's opened by an application that you haven't written then how do you stop it from opening in the registered application? – Lee Willis Mar 18 '14 at 14:59
  • 1
    i think use this way: after entered file`s fullname and password i do "FileSystemRights.ReadData, AccessControlType.Deny". when i trying open this file(with notepad for example), my app show me password dialog and if password true, i changed acl again and next try opening file will succsess. P.S. sorry for my hard english – mechanikos Mar 18 '14 at 15:10