FileSystemWatcher 不从 Office 文件更改中触发事件

FileSystemWatcher not firing Events from Office file changes

我正在尝试使用 FileSystemWatcher 镜像两个目录,但我遇到了 Office 文件的障碍。当 document/spreadsheet 等发生变化时,我似乎无法获得事件。这并不是说我没有收到任何事件,因为我知道办公室应用程序经常使用临时文件。

这是我看到的示例,使用以下代码:

// 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中使用断点):

I create the word document

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

I open the word document

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

I edit and then save the word document

  1. Created ~WRD0000.tmp

I make more edits and then saves the word document

没有事件...

我真的不太明白这是怎么回事。正在更新原始 docx 文件,但我没有看到任何重命名事件或修改。我在这里遗漏了什么吗?

需要设置文档here indicates the Filter属性

甚至文档下面的示例似乎也明确设置了 Filter 属性

引自那里

To watch for changes in all files, set the Filter property to an empty string ("") or use wildcards ("."). To watch a specific file, set the Filter property to the file name. For example, to watch for changes in the file MyDoc.txt, set the Filter property to "MyDoc.txt". You can also watch for changes in a certain type of file. For example, to watch for changes in text files, set the Filter property to "*.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

如果您 运行 此示例代码并看到与我类似的(和预期的)输出, 你可以修改你的代码来使用它。