如何:配置 FileSystemWatcher 组件实例
更新:2007 年 11 月
可以为 FileSystemWatcher 组件实例设置多个属性以确定这些实例的行为。这些属性确定组件实例监视的目录和子目录,以及在目录中发生的将引发事件的确切事情。
指定要监视的目录
可以使用以下两个属性来确定 FileSystemWatcher 组件应当监视哪些目录:Path 和 IncludeSubdirectories。
指示要监视的根目录的完全限定路径
可以针对现有的组件实例设置 Path 属性,如下面的代码所示:
Dim MyWatcher As New System.IO.FileSystemWatcher() MyWatcher.Path = "c:\"
System.IO.FileSystemWatcher MyWatcher = new System.IO.FileSystemWatcher(); MyWatcher.Path = "c:\\";
该设置可以用标准目录路径表示法 (c:\directory) 或 UNC 格式 (\\server\directory) 表示。
IncludeSubdirectories 属性指示是否应监视根目录下的子目录。如果该属性设置为 true,则组件不仅监视主目录中的更改,而且也监视子目录中的相同更改。
指定要监视的更改
除了指定要监视的目录和子目录外,还可以指定有兴趣监视的特定文件和更改类型。在某种程度上,可以通过仅为感兴趣的事件定义处理程序来确定要监视的更改。例如,如果只想在创建文件时得到通知,则可以仅处理 Created 事件。但是,可以设置一系列属性来更进一步限制组件的活动。
可以使用 Filter 属性指示特定文件或通配符匹配模式,将组件限制为仅监视根目录中的某些文件或目录。例如,如果要监视根目录中文本文件的创建,可以将 Filter 属性设置为“*.txt”,然后创建 Created 事件的处理程序。可以将筛选器设置为任何值,包括所有有效的文件扩展名。默认值“*.*”将返回任何内容;""(空字符串)也可以返回所有文件或目录。不过,一次只能设置一个筛选器字符串。必要时,可以在启动 FileSystemWatcher 以接收事件之后更改筛选器。
说明: |
---|
FileSystemWatcher 组件用于监视目录内的更改,而不是监视目录属性本身的更改。例如,如果正在监视名为 c:\MyProjects 的目录,则该组件将监视该目录内的更改,并不监视该目录本身的更改。 |
此外,还可以使用 NotifyFilter 属性进一步限制要响应的更改。可以将 NotifyFilter 属性设置为监视目录名的更改或/和文件名的更改。另外,NotifyFilter 属性还包含一系列限制 Changed 事件的值。Changed 事件可能会引发大量的事件,因为每次当所涉及的文件有任何更改时就会引发一个事件;对于类似复制和移动文件的一些标准操作,由于文件的大小、上次写入和上次访问等各种属性都要改变,实际上会导致引发多个文件更改事件。
NotifyFilter 属性可以限制将使组件实例引发事件的文件或目录更改的数量。可以设置组件仅在发生如下情况之一时才引发事件:文件名或目录名更改;主目录下某项的属性更改;文件大小更改;上次写入时间或上次访问时间更改;对文件或目录的安全访问权限更改。这些值都是 NotifyFilters 枚举的一部分。可以通过使用 BitOr(对于 Visual Basic)或 |(对于 Visual C#)运算符来设置多个要监视的更改,如下面的示例所示。
说明: |
---|
必须将 EnableRaisingEvents 属性设置为 true,才能使 FileSystemWatcher 组件开始监视所做的更改。 |
配置 FileSystemWatcher 组件的实例
创建 FileSystemWatcher 组件的实例。有关更多信息,请参见 如何:创建 FileSystemWatcher 组件实例。
将 Path 属性设置为要监视的根目录的完全限定路径。
提示: 可以以标准文件路径或 UNC 路径的形式输入此路径。
为实例设置任何可选属性。
方案
属性
值
监视对根目录中特定文件或子目录名称的更改
限制实例的监视活动的通配符筛选器表达式
监视根目录下的所有子目录中发生的更改
true 或false
在处理 Changed、Renamed、Deleted 或 Created 事件时仅监视对某个文件或子目录进行的特定更改
NotifyFilters 枚举中的任何可用值
通过将 EnableRaisingEvents 属性设置为 true 来启用组件实例。
例如,假设要创建 FileSystemWatcher 实例来监视名为“reports”的放置目录中新的 .txt 文件的创建。同时还要监视现有报告的更改,这些报告在数据变化时动态地重新运行。如下设置组件:
' This needs to be declared in a place where it will not go out of scope. ' For example, it would be a class variable in a form class. Dim MyWatcher As New System.IO.FileSystemWatcher() ' This code would go in one of the initialization methods of the class. MyWatcher.Path = "c:\" ' Watch only for changes to *.txt files. MyWatcher.Filter = "*.txt" MyWatcher.IncludeSubdirectories = False ' Filter for Last Write changes. MyWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite ' Example of watching more than one type of change. MyWatcher.NotifyFilter = _ System.IO.NotifyFilters.LastAccess Or System.IO.NotifyFilters.Size ' Enable the component to begin watching for changes. MyWatcher.EnableRaisingEvents = True
// This needs to be declared in a place where it will not go out of scope. // For example, it would be a class variable in a form class. System.IO.FileSystemWatcher MyWatcher = new System.IO.FileSystemWatcher(); // This code would go in one of the initialization methods of the class. MyWatcher.Path = "c:\\"; // Watch only for changes to *.txt files. MyWatcher.Filter = "*.txt"; MyWatcher.IncludeSubdirectories = false; // Enable the component to begin watching for changes. MyWatcher.EnableRaisingEvents = true; // Filter for Last Write changes. MyWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite; // Example of watching more than one type of change. MyWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite | System.IO.NotifyFilters.Size;