FileSystemWatcher.EnableRaisingEvents Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets a value indicating whether the component is enabled.
public:
property bool EnableRaisingEvents { bool get(); void set(bool value); };
public bool EnableRaisingEvents { get; set; }
[System.IO.IODescription("FSW_Enabled")]
public bool EnableRaisingEvents { get; set; }
member this.EnableRaisingEvents : bool with get, set
[<System.IO.IODescription("FSW_Enabled")>]
member this.EnableRaisingEvents : bool with get, set
Public Property EnableRaisingEvents As Boolean
Property Value
true
if the component is enabled; otherwise, false
. The default is false
. If you are using the component on a designer in Visual Studio 2005, the default is true
.
- Attributes
Exceptions
The FileSystemWatcher object has been disposed.
The current operating system is not Microsoft Windows NT or later.
The directory specified in Path could not be found.
Path has not been set or is invalid.
Examples
The following example creates a FileSystemWatcher to watch the directory specified at run time. The component is set to watch for changes in LastWrite
and LastAccess
time, the creation, deletion, or renaming of text files in the directory. If a file is changed, created, or deleted, the path to the file prints to the console. When a file is renamed, the old and new paths print to the console.
#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
Remarks
The component will not raise events unless you set EnableRaisingEvents to true
.
Note
The component will not watch the specified directory until the Path property has been set and EnableRaisingEvents is true
.
The WaitForChanged method allows event handlers to be invoked to respond to file changes even if this property is set to false
.