【发布时间】:2013-02-08 19:06:38
【问题描述】:
我已经浏览过,但找不到任何关于我正在寻找的信息,如果有其他帖子已经涵盖了这个,那么我很抱歉。
我正在寻求有关代码的帮助,该代码将监视特定文件夹何时被其他人打开或打开所述文件夹下的文件。此时我可以看到用户何时打开和修改任何文件,但如果他们只是打开文件来查看它,即使我添加 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);
}
【问题讨论】:
-
如果文件夹名称是 C:\JUNK,为什么不把它放在 Path 属性中呢? Filter 属性用于筛选文件而不是文件夹
-
@Steve - 根据您的建议,我更改了代码,但在打开文件夹时仍未触发。
-
尝试使用文件系统过滤驱动来跟踪这种变化。其他变体效果不佳。
-
为了快速完成这项工作,我只是依靠 NTFS 安全审计层。
标签: c# .net wpf filesystemwatcher