文件系统观察程序未从 Office 文件更改触发事件



我正在尝试使用FileSystemWatcher来镜像两个目录,但是我在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中使用断点):

我创建单词文档

  1. Created新的Microsoft Word文档.docx
  2. Changed新的Microsoft Word文档.docx

我打开单词文档

  1. Created~$w Microsoft Word 文档.docx
  2. Changed~$w Microsoft Word文档.docx
  3. Changed~$w Microsoft Word 文档.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:tempfolderDoc1.docx created
D:tempfolderDoc1.docx deleted
D:tempfolderDoc1.docx created
D:tempfolder~WRD0000.tmp created
D:tempfolder~WRD0000.tmp changed
D:tempfolder~WRD0000.tmp changed
D:tempfolder~WRD0000.tmp changed
D:tempfolder~WRD0000.tmp changed
D:tempfolder~WRD0000.tmp changed
D:tempfolder~WRD0000.tmp changed
D:tempfolderDoc1.docx renamed to D:tempfolder~WRL0001.tmp
D:tempfolder~WRD0000.tmp renamed to D:tempfolderDoc1.docx
D:tempfolder~WRL0001.tmp changed
D:tempfolderDoc1.docx changed
D:tempfolderDoc1.docx changed
D:tempfolder~WRL0001.tmp changed
D:tempfolder~$Doc1.docx created
D:tempfolder~$Doc1.docx changed
D:tempfolder~WRL0001.tmp deleted

编辑文档

D:tempfolder~WRD0002.tmp created
D:tempfolder~WRD0002.tmp changed
D:tempfolder~WRD0002.tmp changed
D:tempfolder~WRD0002.tmp changed
D:tempfolder~WRD0002.tmp changed
D:tempfolder~WRD0002.tmp changed
D:tempfolder~WRD0002.tmp changed
D:tempfolderDoc1.docx renamed to D:tempfolder~WRL0003.tmp
D:tempfolder~WRD0002.tmp renamed to D:tempfolderDoc1.docx
D:tempfolder~WRL0003.tmp changed
D:tempfolderDoc1.docx changed
D:tempfolderDoc1.docx changed
D:tempfolder~WRL0003.tmp changed
D:tempfolder~WRL0003.tmp deleted

更多编辑

D:tempfolder~WRD0004.tmp created
D:tempfolder~WRD0004.tmp changed
D:tempfolder~WRD0004.tmp changed
D:tempfolder~WRD0004.tmp changed
D:tempfolder~WRD0004.tmp changed
D:tempfolder~WRD0004.tmp changed
D:tempfolder~WRD0004.tmp changed
D:tempfolderDoc1.docx renamed to D:tempfolder~WRL0005.tmp
D:tempfolder~WRD0004.tmp renamed to D:tempfolderDoc1.docx
D:tempfolder~WRL0005.tmp changed
D:tempfolderDoc1.docx changed
D:tempfolderDoc1.docx changed
D:tempfolder~WRL0005.tmp changed
D:tempfolder~WRL0005.tmp deleted

关闭词

D:tempfolder~$Doc1.docx deleted

从资源管理器中删除

D:tempfolderDoc1.docx deleted

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

最新更新