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

时间:2017-03-28 22:19:28

标签: c# .net ms-word filesystemwatcher

我尝试使用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中使用断点):

  

我创建了word文档

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

    我打开word文档

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

      我编辑然后保存word文档

      1. Created ~WDD0000.tmp
      2.   

        我进行了更多编辑,然后保存了word文档

        没有事件......

        我真的不太明白这是如何运作的。原始docx文件正在更新,但我没有看到任何重命名事件或修改。我在这里找不到什么东西?

1 个答案:

答案 0 :(得分:1)

文档here表示需要设置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

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