簡體   English   中英

FileSystemWatcher不觸發Office文件更改中的事件

[英]FileSystemWatcher not firing Events from Office file changes

我正在嘗試使用FileSystemWatcher來鏡像兩個目錄,但是我遇到了Office文件的障礙。 當文檔/電子表格等發生更改時,我似乎無法獲得事件。 這並不是說我沒有收到任何事件,因為我了解Office應用程序經常使用臨時文件。

這是使用以下代碼的示例示例:

// Create the new watcher and hook up events
FileSystemWatcher fsw = new FileSystemWatcher(source);
fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Attributes | NotifyFilters.Size | NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.CreationTime | NotifyFilters.Security;
fsw.IncludeSubdirectories = true;
fsw.InternalBufferSize = 64000;
fsw.Renamed += Fsw_Renamed;
fsw.Error += Fsw_Error;
fsw.Deleted += (o, e) => OnChange(pair, e);
fsw.Changed += (o, e) => OnChange(pair, e);
fsw.Created += (o, e) => OnChange(pair, e);
fsw.EnableRaisingEvents = true;

我看到的事件如下(在onChangeFsw_ErrorFsw_Renamed使用斷點):

我創建word文檔

  1. Created 新的Microsoft Word Document.docx
  2. Changed 新的Microsoft Word Document.docx

我打開word文檔

  1. Created 〜$ w Microsoft Word Document.docx
  2. Changed 〜$ w Microsoft Word Document.docx
  3. Changed 〜$ w Microsoft Word Document.docx

我編輯然后保存Word文檔

  1. Created 〜WRD0000.tmp

我進行更多編輯,然后保存Word文檔

沒有活動...

我真的不太明白這是如何工作的。 原始docx文件正在更新,但是沒有看到任何重命名事件或修改。 我在這里想念什么嗎?

這里的文檔指示需要設置Filter屬性

甚至文檔下面的示例似乎也明確設置了Filter屬性

從那里引用

要監視所有文件中的更改,請將Filter屬性設置為空字符串(“”)或使用通配符(“ ”)。 要觀看特定文件,請將Filter屬性設置為文件名。 例如,要監視文件MyDoc.txt中的更改,請將Filter屬性設置為“ MyDoc.txt”。 您還可以監視某種文件類型的更改。 例如,要監視文本文件中的更改,請將Filter屬性設置為“ * .txt”。

您可以嘗試將其設置為*.docx

更新資料

從下面的評論,上面的清除顯然沒有用。

我這樣寫了一個簡單的控制台程序

class Program
{
    static void Main(string[] args)
    {
        var source = "D:\\temp\\folder";

        // Create the new watcher and hook up events
        var fsw = new FileSystemWatcher(source)
        {
            NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Attributes | NotifyFilters.Size | NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.CreationTime | NotifyFilters.Security,
            IncludeSubdirectories = true,
            InternalBufferSize = 64000
        };

        using (fsw)
        {
            fsw.Renamed += (o, e) => Console.WriteLine($"{e.OldFullPath} renamed to {e.FullPath}");
            fsw.Error += (o, e) => Console.WriteLine($"{e}");
            fsw.Deleted += (o, e) => Console.WriteLine($"{e.FullPath} deleted");
            fsw.Changed += (o, e) => Console.WriteLine($"{e.FullPath} changed");
            fsw.Created += (o, e) => Console.WriteLine($"{e.FullPath} created");
            fsw.EnableRaisingEvents = true;

            Console.WriteLine("Ready. Press 'Q' to exit");
            while (Console.ReadKey().KeyChar != 'Q')
            {
            }
        }
    }
}

產生以下輸出

啟動時

Ready. Press 'Q' to exit

創建一個新文件

D:\temp\folder\Doc1.docx created
D:\temp\folder\Doc1.docx deleted
D:\temp\folder\Doc1.docx created
D:\temp\folder\~WRD0000.tmp created
D:\temp\folder\~WRD0000.tmp changed
D:\temp\folder\~WRD0000.tmp changed
D:\temp\folder\~WRD0000.tmp changed
D:\temp\folder\~WRD0000.tmp changed
D:\temp\folder\~WRD0000.tmp changed
D:\temp\folder\~WRD0000.tmp changed
D:\temp\folder\Doc1.docx renamed to D:\temp\folder\~WRL0001.tmp
D:\temp\folder\~WRD0000.tmp renamed to D:\temp\folder\Doc1.docx
D:\temp\folder\~WRL0001.tmp changed
D:\temp\folder\Doc1.docx changed
D:\temp\folder\Doc1.docx changed
D:\temp\folder\~WRL0001.tmp changed
D:\temp\folder\~$Doc1.docx created
D:\temp\folder\~$Doc1.docx changed
D:\temp\folder\~WRL0001.tmp deleted

編輯文件

D:\temp\folder\~WRD0002.tmp created
D:\temp\folder\~WRD0002.tmp changed
D:\temp\folder\~WRD0002.tmp changed
D:\temp\folder\~WRD0002.tmp changed
D:\temp\folder\~WRD0002.tmp changed
D:\temp\folder\~WRD0002.tmp changed
D:\temp\folder\~WRD0002.tmp changed
D:\temp\folder\Doc1.docx renamed to D:\temp\folder\~WRL0003.tmp
D:\temp\folder\~WRD0002.tmp renamed to D:\temp\folder\Doc1.docx
D:\temp\folder\~WRL0003.tmp changed
D:\temp\folder\Doc1.docx changed
D:\temp\folder\Doc1.docx changed
D:\temp\folder\~WRL0003.tmp changed
D:\temp\folder\~WRL0003.tmp deleted

更多編輯

D:\temp\folder\~WRD0004.tmp created
D:\temp\folder\~WRD0004.tmp changed
D:\temp\folder\~WRD0004.tmp changed
D:\temp\folder\~WRD0004.tmp changed
D:\temp\folder\~WRD0004.tmp changed
D:\temp\folder\~WRD0004.tmp changed
D:\temp\folder\~WRD0004.tmp changed
D:\temp\folder\Doc1.docx renamed to D:\temp\folder\~WRL0005.tmp
D:\temp\folder\~WRD0004.tmp renamed to D:\temp\folder\Doc1.docx
D:\temp\folder\~WRL0005.tmp changed
D:\temp\folder\Doc1.docx changed
D:\temp\folder\Doc1.docx changed
D:\temp\folder\~WRL0005.tmp changed
D:\temp\folder\~WRL0005.tmp deleted

接近詞

D:\temp\folder\~$Doc1.docx deleted

從資源管理器中刪除

D:\temp\folder\Doc1.docx deleted

如果您運行此示例代碼並看到與我相似的(和預期的)輸出,則可以修改代碼以使用此代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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