FileSystemWatcher 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當目錄或目錄內的檔案變更時,接聽 (Listen) 檔案系統變更通知並引發事件。
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 內的檔案或目錄重新命名時。 |