检测用户何时打开文件

时间:2016-01-25 17:49:17

标签: c# visual-studio file

我正在寻找可以检测用户何时打开文件的内容(很像防病毒软件),我一直在寻找,但我能找到的就是当用户创建/删除/修改文件时在特定的路径上

2 个答案:

答案 0 :(得分:-1)

您可以使用C#FileWatcher - 在目录或目录中的文件发生更改时,侦听文件系统更改通知并引发事件。

实施例: http://matijabozicevic.com/blog/csharp-net-development/csharp-monitor-directory-activity-using-fileSystemWatcher-class

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.
}

答案 1 :(得分:-1)

尝试使用FileSystemWatcher.Changed事件:https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.changed(v=vs.110).aspx

为了检测它何时打开,您必须将NotifyFilter属性设置为NotifyFilters.LastAccess(这将触发Changed事件,因为文件的LastAccess属性已更改)。

我从未用它来监控整个硬盘,所以YMMV。