【问题标题】:Raising event on reading file?在读取文件时引发事件?
【发布时间】:2011-02-27 08:51:55
【问题描述】:

我有外部应用程序读取文件,我想挂钩它以在我的应用程序中获取事件。但我找不到挂钩 ReadFile 的来源(或其他可以帮助我实现这一目标的东西)。任何想法如何做到这一点?它必须在用户模式下完成。我正在考虑类似于 Process Monitor 的东西。我想知道它是怎么做到的..

【问题讨论】:

    标签: c# c winapi hook


    【解决方案1】:

    在 .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);
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2015-09-06
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多