我尝试使用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;
我看到的事件如下(在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
~WDD0000.tmp 我进行了更多编辑,然后保存了word文档
没有事件......
我真的不太明白这是如何运作的。原始docx
文件正在更新,但我没有看到任何重命名事件或修改。我在这里找不到什么东西?
答案 0 :(得分:1)
即使文档下面的示例似乎也明确设置了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
如果您运行此示例代码并看到类似(和预期)的输出,就像我一样 你可以修改你的代码来使用它。