FileSystemWatcher.Changed 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当更改指定 Path 中的文件和目录时发生。
public:
event System::IO::FileSystemEventHandler ^ Changed;
public event System.IO.FileSystemEventHandler? Changed;
public event System.IO.FileSystemEventHandler Changed;
[System.IO.IODescription("FSW_Changed")]
public event System.IO.FileSystemEventHandler Changed;
member this.Changed : System.IO.FileSystemEventHandler
[<System.IO.IODescription("FSW_Changed")>]
member this.Changed : System.IO.FileSystemEventHandler
Public Custom Event Changed As FileSystemEventHandler
事件类型
- 属性
示例
以下示例使用 Changed 事件在所监视文件发生更改时显示控制台的文件路径。
#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
注解
当 Changed 对所监视目录中文件或目录的大小、系统属性、上次写入时间、上次访问时间或安全权限进行更改时,将引发 事件。
注意
常见的文件系统操作可能会引发多个事件。 例如,当文件从一个目录移动到另一个目录时,可能会引发多个 OnChanged 和一些 OnCreated 和 OnDeleted 事件。 移动文件是一项复杂的操作,由多个简单操作组成,因此引发多个事件。 同样,某些应用程序 (例如,防病毒软件) 可能会导致 检测到 FileSystemWatcher的其他文件系统事件。
用于 NotifyFilter 限制处理此事件时引发的通知数。
注意
当 属性不是 null
时SynchronizingObject,Changed相对于其他FileSystemWatcher事件引发事件的顺序可能会更改。