C# FileSystemWatcher未从Office文件更改触发事件
我试图使用C# FileSystemWatcher未从Office文件更改触发事件,c#,.net,ms-word,filesystemwatcher,C#,.net,Ms Word,Filesystemwatcher,我试图使用FileSystemWatcher镜像两个目录,但我遇到了Office文件的障碍。当文档/电子表格等发生更改时,我似乎无法获取事件。但这并不是说我没有收到任何事件,因为我知道office应用程序经常使用临时文件 下面是我看到的示例,使用以下代码: // Create the new watcher and hook up events FileSystemWatcher fsw = new FileSystemWatcher(source); fsw.NotifyFilter = No
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\u Error
和Fsw\u重命名中的断点):
我创建word文档
已创建
新的Microsoft Word文档.docx
已更改
新的Microsoft Word文档.docx
我打开word文档
已创建
~$w Microsoft Word Document.docx
已更改
~$w Microsoft Word Document.docx
已更改
~$w Microsoft Word Document.docx
我编辑并保存word文档
已创建
~WRD0000.tmp
我进行了更多编辑,然后保存word文档
无事件…
我真的不太明白这是怎么回事。原始的docx
文件正在更新,但我没有看到任何重命名事件或修改。这里缺少什么吗?文档表明需要设置属性
甚至文档下面的示例似乎也明确设置了过滤器
属性
引述
要监视所有文件中的更改,请将筛选器属性设置为空字符串(“”)或使用通配符(“”)。要查看特定文件,请将过滤器属性设置为文件名。例如,要查看文件MyDoc.txt中的更改,请将过滤器属性设置为“MyDoc.txt”。您还可以监视特定类型文件中的更改。例如,要查看文本文件中的更改,请将过滤器属性设置为“*.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')
{
}
}
}
}
这将产生以下输出
发射时
创建新文档
编辑文档
更多编辑
结束语
从资源管理器中删除
如果您运行此示例代码并看到与我类似的(和预期的)输出,
您可以修改代码以使用此选项 文档表明需要设置属性
甚至文档下面的示例似乎也明确设置了过滤器
属性
引述
要监视所有文件中的更改,请将筛选器属性设置为空字符串(“”)或使用通配符(“”)。要查看特定文件,请将过滤器属性设置为文件名。例如,要查看文件MyDoc.txt中的更改,请将过滤器属性设置为“MyDoc.txt”。您还可以监视特定类型文件中的更改。例如,要查看文本文件中的更改,请将过滤器属性设置为“*.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')
{
}
}
}
}
这将产生以下输出
发射时
创建新文档
编辑文档
更多编辑
结束语
从资源管理器中删除
如果您运行此示例代码并看到与我类似的(和预期的)输出,
您可以修改代码以使用此选项 在深入研究这一点之后,我认为我的其他一些代码一定是错的——特别是我稍后在ConcurrentQueue中所做的某些事情,不知何故阻止了我从FSW接收事件。我还不明白,但我会接受这一点,因为它清楚地演示了如何将FSW与office文档一起使用。在深入研究这一点之后,我认为我的一些其他代码一定是错误的——特别是我稍后在ConcurrentQueue中所做的某件事,不知何故阻止了我从FSW接收事件。我还不明白,但我会接受这一点,因为它清楚地展示了如何在office文档中使用FSW。
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