[英]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;
我看到的事件如下(在onChange
, Fsw_Error
和Fsw_Renamed
使用斷點):
我創建word文檔
Created
新的Microsoft Word Document.docx Changed
新的Microsoft Word Document.docx 我打開word文檔
Created
〜$ w Microsoft Word Document.docx Changed
〜$ w Microsoft Word Document.docx Changed
〜$ w Microsoft Word Document.docx 我編輯然后保存Word文檔
Created
〜WRD0000.tmp 我進行更多編輯,然后保存Word文檔
沒有活動...
我真的不太明白這是如何工作的。 原始docx
文件正在更新,但是沒有看到任何重命名事件或修改。 我在這里想念什么嗎?
甚至文檔下面的示例似乎也明確設置了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.