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

Sar*_*ren 7 .net c# wpf filesystemwatcher

我浏览过但却无法找到有关我所寻求的任何信息,如果还有其他帖子已经过去,那么我道歉.

我正在寻找帮助代码,该代码将监视特定文件夹,以便在其他人打开文件夹时或打开所述文件夹下的文件时.此时我可以看到用户打开并修改任何文件的时间,但如果他们只是打开文件来查看它,即使我添加LastAccessed也不会抛出事件.任何信息或帮助将不胜感激.

文件夹名称是C:\ Junk

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);
    }
Run Code Online (Sandbox Code Playgroud)

Vla*_*adL 0

你应该设置

watcher.Path = @"C:\junk";
Run Code Online (Sandbox Code Playgroud)

watcher.Filter如果事件应该为所有文件触发,则删除行

使用Filter属性,您可以为匹配文件设置通配符,例如*.txt