[英]Raising event on reading file?
我有外部应用程序正在读取文件,我想将其挂钩以在我的应用程序中获取事件。 但是我找不到钩住ReadFile的资源(或其他可以帮助我实现该目标的东西)。 任何想法如何做到这一点? 必须在用户模式下完成。 我在考虑类似于Process Monitor的东西。 我不知道它是如何做到的。
在.net中,您可以使用FileSystemWatcher 。 您将需要为Changed事件添加一个处理程序,该处理程序将检测上次访问时间的更改(以及其他情况)。
从上面链接的MSDN示例中:
public static void Foo()
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"Your path";
/* Watch for changes in LastAccess time */
watcher.NotifyFilter = NotifyFilters.LastAccess;
// Only watch text files.
watcher.Filter = "*.txt";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.