FileSystemWatcher Costruttori
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Inizializza una nuova istanza della classe FileSystemWatcher.
Overload
| Nome | Descrizione |
|---|---|
| FileSystemWatcher() |
Inizializza una nuova istanza della classe FileSystemWatcher. |
| FileSystemWatcher(String) |
Inizializza una nuova istanza della FileSystemWatcher classe , data la directory specificata da monitorare. |
| FileSystemWatcher(String, String) |
Inizializza una nuova istanza della FileSystemWatcher classe , in base alla directory e al tipo di file specificati da monitorare. |
FileSystemWatcher()
- Origine:
- FileSystemWatcher.cs
- Origine:
- FileSystemWatcher.cs
- Origine:
- FileSystemWatcher.cs
- Origine:
- FileSystemWatcher.cs
- Origine:
- FileSystemWatcher.cs
Inizializza una nuova istanza della classe FileSystemWatcher.
public:
FileSystemWatcher();
public FileSystemWatcher();
Public Sub New ()
Esempio
Nell'esempio seguente viene creato un FileSystemWatcher oggetto per controllare la directory specificata in fase di esecuzione. L'oggetto FileSystemWatcher controlla le modifiche apportate LastWrite e LastAccess le ore e per la creazione, l'eliminazione o la ridenominazione dei file di testo nella directory. Se un file viene modificato, creato o eliminato, il percorso del file viene visualizzato nella console. Quando un file viene rinominato, i percorsi precedenti e nuovi vengono visualizzati nella console.
In questo esempio vengono usati gli System.Diagnostics spazi dei nomi e System.IO .
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
Commenti
Non è possibile guardare un computer remoto che non dispone di Windows NT o Windows 2000. Non è possibile guardare un computer Windows NT 4.0 remoto da un computer Windows NT 4.0.
Nella tabella seguente vengono illustrati i valori iniziali delle proprietà per un'istanza di FileSystemWatcher.
| Proprietà | Valore iniziale |
|---|---|
| NotifyFilter | combinazione OR bit per bit di LastWrite, FileNamee DirectoryName |
| EnableRaisingEvents | false |
| Filter | "*.*" (Guarda tutti i file. |
| IncludeSubdirectories | false |
| InternalBufferSize | 8192 |
| Path | stringa vuota ("") |
Annotazioni
Il componente non controlla la directory specificata fino a quando non Path viene impostato e EnableRaisingEvents è true.
Vedi anche
- NotifyFilters
- FileSystemEventArgs
- FileSystemEventHandler
- InternalBufferOverflowException
- Path
- RenamedEventArgs
- RenamedEventHandler
- WaitForChangedResult
- WatcherChangeTypes
Si applica a
FileSystemWatcher(String)
- Origine:
- FileSystemWatcher.cs
- Origine:
- FileSystemWatcher.cs
- Origine:
- FileSystemWatcher.cs
- Origine:
- FileSystemWatcher.cs
- Origine:
- FileSystemWatcher.cs
Inizializza una nuova istanza della FileSystemWatcher classe , data la directory specificata da monitorare.
public:
FileSystemWatcher(System::String ^ path);
public FileSystemWatcher(string path);
new System.IO.FileSystemWatcher : string -> System.IO.FileSystemWatcher
Public Sub New (path As String)
Parametri
- path
- String
Directory da monitorare, in notazione UNC (Universal Naming Convention).
Eccezioni
Il path parametro è null.
Il path parametro è una stringa vuota ("").
oppure
Il percorso specificato tramite il path parametro non esiste.
path è troppo lungo.
Commenti
Annotazioni
Il componente non controlla la directory specificata fino a quando non Path viene impostato e EnableRaisingEvents è true.
Il componente può controllare i file nel computer personale, in un'unità di rete o in un computer remoto.
Non è possibile guardare un computer remoto che non dispone di Windows NT o Windows 2000. Non è possibile guardare un computer Windows NT 4.0 remoto da un computer Windows NT 4.0. La Filter proprietà è impostata per impostazione predefinita per controllare tutti i file.
Vedi anche
- NotifyFilters
- FileSystemEventArgs
- FileSystemEventHandler
- Filter
- InternalBufferOverflowException
- Path
- RenamedEventArgs
- RenamedEventHandler
- WaitForChangedResult
- WatcherChangeTypes
Si applica a
FileSystemWatcher(String, String)
- Origine:
- FileSystemWatcher.cs
- Origine:
- FileSystemWatcher.cs
- Origine:
- FileSystemWatcher.cs
- Origine:
- FileSystemWatcher.cs
- Origine:
- FileSystemWatcher.cs
Inizializza una nuova istanza della FileSystemWatcher classe , in base alla directory e al tipo di file specificati da monitorare.
public:
FileSystemWatcher(System::String ^ path, System::String ^ filter);
public FileSystemWatcher(string path, string filter);
new System.IO.FileSystemWatcher : string * string -> System.IO.FileSystemWatcher
Public Sub New (path As String, filter As String)
Parametri
- path
- String
Directory da monitorare, in notazione UNC (Universal Naming Convention).
- filter
- String
Tipo di file da controllare. Ad esempio, "*.txt" controlla le modifiche a tutti i file di testo.
Eccezioni
Il path parametro è una stringa vuota ("").
oppure
Il percorso specificato tramite il path parametro non esiste.
path è troppo lungo.
Commenti
Annotazioni
Il componente non controlla la directory specificata fino a quando non Path viene impostato e EnableRaisingEvents è true.
Il componente può controllare i file nel computer personale, in un'unità di rete o in un computer remoto.
Non è possibile guardare un computer remoto che non dispone di Windows NT o Windows 2000. Non è possibile guardare un computer Windows NT 4.0 remoto da un computer Windows NT 4.0.
Vedi anche
- NotifyFilters
- FileSystemEventArgs
- FileSystemEventHandler
- Filter
- InternalBufferOverflowException
- Path
- RenamedEventArgs
- RenamedEventHandler
- WaitForChangedResult
- WatcherChangeTypes