FileSystemWatcher的用于监视的文件夹/文件打开文件夹、文件、FileSystemWatcher

2023-09-02 21:08:22 作者:烙印

我已经浏览四周,但找不到我所寻找的任何信息,如果已经达到这个话,我道歉另一篇文章。

我寻求帮助,code,将监视时,文件夹是由其他人打开或者在一个文件夹说打开特定文件夹。在这一点上,当用户打开和修改任何文件,但如果他们只是打开该文件进行查看,它不会抛出,即使我想补充上次访问的事件,我可以看到。任何信息或帮助将是AP preciated。

文件夹名称是C:垃圾

code在C#4.0:

  [PermissionSet中(SecurityAction.Demand,名称=FullTrust的)
    公共静态无效的run()
    {


        FileSystemWatcher的观察家=新FileSystemWatcher的();
        watcher.Path = @C:;
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        watcher.Filter =垃圾;

        //添加事件处理程序。
        watcher.Changed + =新FileSystemEventHandler(调用onChanged);
        watcher.Created + =新FileSystemEventHandler(调用onChanged);
        watcher.Deleted + =新FileSystemEventHandler(调用onChanged);
        watcher.Renamed + =新RenamedEventHandler(OnRenamed);
        watcher.IncludeSubdirectories = TRUE;
        watcher.EnableRaisingEvents = TRUE;

        //等待退出该程序的用户。
        Console.WriteLine(preSS 'Q '退出样本。);
        而(Console.Read()=Q!);
    }

    //定义事件处理程序。
    私有静态无效调用onChanged(对象源,FileSystemEventArgs E)
    {
        //指定做了什么时,文件被更改,创建或删除。
        Console.WriteLine(文件:+ e.FullPath ++ e.ChangeType);
    }

    私有静态无效OnRenamed(对象源,RenamedEventArgs E)
    {
        //指定什么,当一个文件被重命名完成。
        Console.WriteLine(文件:{0}重命名为{1},e.OldFullPath,e.FullPath);
    }
 

解决方案   

不抛出一个事件,甚至当我加入上次访问。

由于 NotifyFilters.LastAccessed 指定您希望中检索该属性,而不是事件订阅。可用的事件是更改创建删除,其中没有你想要做什么。

利用 C 中的 FileSystemWatcher 制作一个文件夹监控小工具

您应该看一看在 ReadDirectoryChangesW Win32函数,记录的此处。它可以通过一个 FILE_NOTIFY_CHANGE_LAST_ACCESS 标记,这似乎能够提供你想要的:

  

任何更改都在看着目录或子树的文件的最后访问时间引起的变化通知给等待操作返回。

编辑:无视这一点, FileSystemWatcher的并内部传递 NotifyFilters.LastWrite 为INT 32,这是相同的为 FILE_NOTIFY_CHANGE_LAST_ACCESS ,到 ReadDirectoryChangesW 。该功能之后,还对文件访问不会通知,我已经尽力了。

也许这是由这个:

  

上次访问时间有一个宽松的粒度,只有保证时间精确到一小时之内。在Windows Vista中,我们已经禁用更新上次访问时间,提高NTFS的性能。如果您使用的是依赖于该值的应用程序,可以使用下面的命令启用它:

  FSUTIL行为集disablelastaccess 0
 

您必须重新启动计算机使更改生效。

如果您执行的命令提示符下,那么也许在 LastAccess 将被写入和事件将触发。我不打算尝试在我的SSD和不具备虚拟机准备好了,但在Windows 7 disablelastaccess 似乎被激活了的开箱即用。

如果它仍然会在禁用这种行为不工作,等待Raymond Chen的建议箱(或自己)来用,平时还有为什么文档似乎并没有正确地描述您遇到的行为相当合乎逻辑的解释。 ; - )

您可能也只是扫描目录循环,并期待在文件的LastAccessed属性。什么是你想做的在的用户打开某些文件?

I have browsed around but cannot find any information on what I am seeking, if there is another post that already goes over this then I apologize.

I am seeking help with code that will monitor a specific folder for when the folder is opened by another person or when a file under said folder is opened. At this point I can see when a user opens and modifies any files but if they just open the file to view it, it does not throw an event even when I add LastAccessed. Any information or help would be appreciated.

Folder name is C:Junk

Code in C# 4.0:

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public static void Run()
    {


        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = @"C:";
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        watcher.Filter = "junk";

        // Add event handlers.
        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;

        // Wait for the user to quit the program.
        Console.WriteLine("Press 'q' to quit the sample.");
        while (Console.Read() != 'q') ;
    }

    // Define the event handlers. 
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
        Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
    }

    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        // Specify what is done when a file is renamed.
        Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
    }

解决方案

it does not throw an event even when I add LastAccessed.

Because NotifyFilters.LastAccessed specifies that you wish to retreive that property, not the event to subscribe to. The available events are Changed, Created, or Deleted, none of which does what you want.

You should take a look at the ReadDirectoryChangesW Win32 function, documented here. It can be passed a FILE_NOTIFY_CHANGE_LAST_ACCESS flag, which seems to deliver what you want:

Any change to the last access time of files in the watched directory or subtree causes a change notification wait operation to return.

Edit: disregard this, the FileSystemWatcher does internally pass NotifyFilters.LastWrite as int 32, which is the same as FILE_NOTIFY_CHANGE_LAST_ACCESS, to ReadDirectoryChangesW. That function then still does not notify on file access, I've tried.

Perhaps this is caused by this:

Last Access Time has a loose granularity that only guarantees that the time is accurate to within one hour. In Windows Vista, we've disabled updates to Last Access Time to improve NTFS performance. If you are using an application that relies on this value, you can enable it using the following command:

fsutil behavior set disablelastaccess 0

You must restart the computer for this change to take effect.

If you execute that on the command prompt, perhaps then the LastAccess will be written and the event will fire. I'm not going to try in on my SSD and don't have a VM ready, but on Windows 7 disablelastaccess seems to be enabled out-of-the-box.

If it still doesn't work when you disable that behavior, wait for Raymond Chen's suggestion box (or himself) to come by, usually there's a quite logical explanation for why the documentation does not seem to correctly describe the behaviour you encounter. ;-)

You may as well just scan the directory in a loop and look at the LastAccessed property of the Files. What are you trying to do when a user opens a certain file?