I am looking for something that detects when a user opens a file(much like anti-virus software), i've been looking, but all i could find is when a user creates/deletes/modifies a file on a specific path
Asked
Active
Viewed 349 times
-1
-
I can't get much out of that :/ – stefan Jan 25 '16 at 17:54
-
[`FileSystemWatcher` class](https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher)? – Uwe Keim Jan 25 '16 at 17:55
-
2@UweKeim OP is asking for something different. When a user opens a file. – Sriram Sakthivel Jan 25 '16 at 17:56
-
1@UweKeim I think FileSystemWatcher only triggers on changed, created, deleted, and renamed. I don't think it notifies on open/lock. – tehDorf Jan 25 '16 at 17:56
2 Answers
-1
You can use C# FileWatcher - Listens to the file system change notifications and raises events when a directory, or file in a directory, changes.
Using FileSystemWatcher to monitor a directory
MSDN: https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx
FileSystemWatcher watcher;
private void watch()
{
watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
//Copies file to another directory.
}
-
1It has onchanged() oncreated() ondeleted() , but nothing for when a user opens a file? – stefan Jan 25 '16 at 18:00
-
1
-
with NotifyFilters.LastAccess we can detect file open event also.. Did you check? – null1941 Jan 25 '16 at 18:06
-
-
the snipped mentioned here is enough. You can add directly into your solution and check it.. – null1941 Jan 25 '16 at 18:10
-
I tried it, and it doesn't launch the event when i open a file in the given path( i tried C:\ and C:\windows) – stefan Jan 25 '16 at 18:18
-1
Try using the FileSystemWatcher.Changed
event: https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.changed(v=vs.110).aspx
In order to detect when it is opened, you must set the NotifyFilter
property to NotifyFilters.LastAccess
(this will trigger the Changed
event because the LastAccess property of the file is changed).
I've never used it to monitor an entire HDD, so YMMV.

solidau
- 4,021
- 3
- 24
- 45