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.

4

2 回答 2

2

我认为没有一个简单的答案可以在 C# 中实现。

这个问题中有一些信息,但都是相当低级的东西......文件系统过滤器驱动程序和API挂钩!

您想监视文件夹中的一小部分文件还是整个磁盘?

于 2014-03-18T14:31:16.783 回答
2

System.IO.FileSystemWatcher 是为监视文件更改而构建的。由于“打开文件”不会更改文件本身,因此它肯定不会起作用。

有可能的方法来做到这一点

选项1

您可以通过 WMI 查询(如果您的操作系统是基于 Windows 的)或通过打开文件进行写入(文件正在使用时发生异常)来检测文件是否正在使用。

选项 2

您可以尝试获取文件的句柄,并查看哪个进程拥有所有权。如果是这样,则该文件已被其他人打开。检查这篇文章使用 C#,如何找出锁定文件的进程?

于 2014-03-18T14:19:47.790 回答