簡體   English   中英

在讀取文件時引發事件?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM