【发布时间】:2014-07-03 13:17:59
【问题描述】:
我的目标是设置一个服务来监视一个包含大约 200 个 .exe 文件的网络文件夹。我想要的是让服务在每次启动一个 .exe 时更新一个日志。基本上我想通过记录每个应用程序的每次使用来记录每个应用程序的使用情况。
我尝试使用 FileSystemWatcher 类来完成此操作,代码如下,认为 LastAccess 过滤器可以解决问题,但似乎不会。当我运行此代码时,打开应用程序时不会引发任何事件。
有什么方法可以使用 FileSysteWatcher 类来进行这种监控吗?有什么办法可以做我正在尝试的事情吗?
Private Sub StartWatch()
Dim exeWatcher As New FileSystemWatcher
exeWatcher.Path = "<path>"
exeWatcher.Filter = "*.exe"
exeWatcher.IncludeSubdirectories = True
exeWatcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.Attributes)
AddHandler exeWatcher.Changed, AddressOf ExeChanged
exeWatcher.EnableRaisingEvents = True
End Sub
Private Sub ExeChanged(source As Object, e As FileSystemEventArgs)
Console.WriteLine("File: " & e.FullPath & " " & DateTime.Now.ToString())
End Sub
【问题讨论】:
-
一句话,没有。文件系统跟踪文件本身,而不是操作系统对它们的处理;执行不是“由文件”执行的,文件被复制到 RAM 并在那里执行。有点..大声笑
-
@Grim 这是一个有用的区别。谢谢。
标签: vb.net