FileSystemWatcher 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
侦听文件系统更改通知,并在目录或目录中的文件发生更改时引发事件。
public ref class FileSystemWatcher : System::ComponentModel::Component, System::ComponentModel::ISupportInitialize
public ref class FileSystemWatcher : IDisposable
public class FileSystemWatcher : System.ComponentModel.Component, System.ComponentModel.ISupportInitialize
public class FileSystemWatcher : IDisposable
[System.IO.IODescription("FileSystemWatcherDesc")]
public class FileSystemWatcher : System.ComponentModel.Component, System.ComponentModel.ISupportInitialize
type FileSystemWatcher = class
inherit Component
interface ISupportInitialize
type FileSystemWatcher = class
interface IDisposable
[<System.IO.IODescription("FileSystemWatcherDesc")>]
type FileSystemWatcher = class
inherit Component
interface ISupportInitialize
Public Class FileSystemWatcher
Inherits Component
Implements ISupportInitialize
Public Class FileSystemWatcher
Implements IDisposable
- 继承
- 继承
-
FileSystemWatcher
- 属性
- 实现
示例
以下示例创建 ,FileSystemWatcher以watch在运行时指定的目录。 组件设置为watch,以便更改LastWrite
和LastAccess
时间,以及创建、删除或重命名目录中的文本文件。 如果更改、创建或删除了某个文件,该文件的路径将输出到控制台。 重命名文件时,新旧路径将打印到控制台。
#include "pch.h"
using namespace System;
using namespace System::IO;
class MyClassCPP
{
public:
int static Run()
{
FileSystemWatcher^ watcher = gcnew FileSystemWatcher("C:\\path\\to\\folder");
watcher->NotifyFilter = static_cast<NotifyFilters>(NotifyFilters::Attributes
| NotifyFilters::CreationTime
| NotifyFilters::DirectoryName
| NotifyFilters::FileName
| NotifyFilters::LastAccess
| NotifyFilters::LastWrite
| NotifyFilters::Security
| NotifyFilters::Size);
watcher->Changed += gcnew FileSystemEventHandler(MyClassCPP::OnChanged);
watcher->Created += gcnew FileSystemEventHandler(MyClassCPP::OnCreated);
watcher->Deleted += gcnew FileSystemEventHandler(MyClassCPP::OnDeleted);
watcher->Renamed += gcnew RenamedEventHandler(MyClassCPP::OnRenamed);
watcher->Error += gcnew ErrorEventHandler(MyClassCPP::OnError);
watcher->Filter = "*.txt";
watcher->IncludeSubdirectories = true;
watcher->EnableRaisingEvents = true;
Console::WriteLine("Press enter to exit.");
Console::ReadLine();
return 0;
}
private:
static void OnChanged(Object^ sender, FileSystemEventArgs^ e)
{
if (e->ChangeType != WatcherChangeTypes::Changed)
{
return;
}
Console::WriteLine("Changed: {0}", e->FullPath);
}
static void OnCreated(Object^ sender, FileSystemEventArgs^ e)
{
Console::WriteLine("Created: {0}", e->FullPath);
}
static void OnDeleted(Object^ sender, FileSystemEventArgs^ e)
{
Console::WriteLine("Deleted: {0}", e->FullPath);
}
static void OnRenamed(Object^ sender, RenamedEventArgs^ e)
{
Console::WriteLine("Renamed:");
Console::WriteLine(" Old: {0}", e->OldFullPath);
Console::WriteLine(" New: {0}", e->FullPath);
}
static void OnError(Object^ sender, ErrorEventArgs^ e)
{
PrintException(e->GetException());
}
static void PrintException(Exception^ ex)
{
if (ex != nullptr)
{
Console::WriteLine("Message: {0}", ex->Message);
Console::WriteLine("Stacktrace:");
Console::WriteLine(ex->StackTrace);
Console::WriteLine();
PrintException(ex->InnerException);
}
}
};
int main()
{
MyClassCPP::Run();
}
using System;
using System.IO;
namespace MyNamespace
{
class MyClassCS
{
static void Main()
{
using var watcher = new FileSystemWatcher(@"C:\path\to\folder");
watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
watcher.Changed += OnChanged;
watcher.Created += OnCreated;
watcher.Deleted += OnDeleted;
watcher.Renamed += OnRenamed;
watcher.Error += OnError;
watcher.Filter = "*.txt";
watcher.IncludeSubdirectories = true;
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
private static void OnChanged(object sender, FileSystemEventArgs e)
{
if (e.ChangeType != WatcherChangeTypes.Changed)
{
return;
}
Console.WriteLine($"Changed: {e.FullPath}");
}
private static void OnCreated(object sender, FileSystemEventArgs e)
{
string value = $"Created: {e.FullPath}";
Console.WriteLine(value);
}
private static void OnDeleted(object sender, FileSystemEventArgs e) =>
Console.WriteLine($"Deleted: {e.FullPath}");
private static void OnRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"Renamed:");
Console.WriteLine($" Old: {e.OldFullPath}");
Console.WriteLine($" New: {e.FullPath}");
}
private static void OnError(object sender, ErrorEventArgs e) =>
PrintException(e.GetException());
private static void PrintException(Exception? ex)
{
if (ex != null)
{
Console.WriteLine($"Message: {ex.Message}");
Console.WriteLine("Stacktrace:");
Console.WriteLine(ex.StackTrace);
Console.WriteLine();
PrintException(ex.InnerException);
}
}
}
}
Imports System.IO
Namespace MyNamespace
Class MyClassVB
Shared Sub Main()
Using watcher = New FileSystemWatcher("C:\path\to\folder")
watcher.NotifyFilter = NotifyFilters.Attributes Or
NotifyFilters.CreationTime Or
NotifyFilters.DirectoryName Or
NotifyFilters.FileName Or
NotifyFilters.LastAccess Or
NotifyFilters.LastWrite Or
NotifyFilters.Security Or
NotifyFilters.Size
AddHandler watcher.Changed, AddressOf OnChanged
AddHandler watcher.Created, AddressOf OnCreated
AddHandler watcher.Deleted, AddressOf OnDeleted
AddHandler watcher.Renamed, AddressOf OnRenamed
AddHandler watcher.Error, AddressOf OnError
watcher.Filter = "*.txt"
watcher.IncludeSubdirectories = True
watcher.EnableRaisingEvents = True
Console.WriteLine("Press enter to exit.")
Console.ReadLine()
End Using
End Sub
Private Shared Sub OnChanged(sender As Object, e As FileSystemEventArgs)
If e.ChangeType <> WatcherChangeTypes.Changed Then
Return
End If
Console.WriteLine($"Changed: {e.FullPath}")
End Sub
Private Shared Sub OnCreated(sender As Object, e As FileSystemEventArgs)
Dim value As String = $"Created: {e.FullPath}"
Console.WriteLine(value)
End Sub
Private Shared Sub OnDeleted(sender As Object, e As FileSystemEventArgs)
Console.WriteLine($"Deleted: {e.FullPath}")
End Sub
Private Shared Sub OnRenamed(sender As Object, e As RenamedEventArgs)
Console.WriteLine($"Renamed:")
Console.WriteLine($" Old: {e.OldFullPath}")
Console.WriteLine($" New: {e.FullPath}")
End Sub
Private Shared Sub OnError(sender As Object, e As ErrorEventArgs)
PrintException(e.GetException())
End Sub
Private Shared Sub PrintException(ex As Exception)
If ex IsNot Nothing Then
Console.WriteLine($"Message: {ex.Message}")
Console.WriteLine("Stacktrace:")
Console.WriteLine(ex.StackTrace)
Console.WriteLine()
PrintException(ex.InnerException)
End If
End Sub
End Class
End Namespace
注解
有关此 API 的详细信息,请参阅 FileSystemWatcher 的补充 API 备注。
构造函数
FileSystemWatcher() |
初始化 FileSystemWatcher 类的新实例。 |
FileSystemWatcher(String) |
在给定要监视的指定目录的情况下,初始化 FileSystemWatcher 类的新实例。 |
FileSystemWatcher(String, String) |
在给定要监视的指定目录和文件类型的情况下,初始化 FileSystemWatcher 类的新实例。 |
属性
CanRaiseEvents |
获取一个指示组件是否可以引发事件的值。 (继承自 Component) |
Container |
获取包含 IContainer 的 Component。 (继承自 Component) |
DesignMode |
获取一个值,用以指示 Component 当前是否处于设计模式。 (继承自 Component) |
EnableRaisingEvents |
获取或设置一个值,该值指示是否启用此组件。 |
Events |
获取附加到此 Component 的事件处理程序的列表。 (继承自 Component) |
Filter |
获取或设置用于确定在目录中监视哪些文件的筛选器字符串。 |
Filters |
获取用于确定在目录中监视哪些文件的所有筛选器的集合。 |
IncludeSubdirectories |
获取或设置一个值,该值指示是否应监视指定路径中的子目录。 |
InternalBufferSize |
获取或设置内部缓冲区的大小(以字节为单位)。 |
NotifyFilter |
获取或设置要监视的更改的类型。 |
Path |
获取或设置要监视的目录的路径。 |
Site |
获取或设置 ISite 的 FileSystemWatcher。 |
SynchronizingObject |
获取或设置用于封送因目录更改而发出的事件处理程序调用的对象。 |
方法
事件
Changed |
当更改指定 Path 中的文件和目录时发生。 |
Created |
当在指定 Path 中创建文件和目录时发生。 |
Deleted |
删除指定 Path 中的文件或目录时发生。 |
Disposed |
在通过调用 Dispose() 方法释放组件时发生。 (继承自 Component) |
Error |
当 FileSystemWatcher 的实例无法继续监视更改或内部缓冲区溢出时发生。 |
Renamed |
重命名指定 Path 中的文件或目录时发生。 |