FileSystemWatcher Konstruktory
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Inicjuje nowe wystąpienie klasy FileSystemWatcher.
Przeciążenia
| Nazwa | Opis |
|---|---|
| FileSystemWatcher() |
Inicjuje nowe wystąpienie klasy FileSystemWatcher. |
| FileSystemWatcher(String) |
Inicjuje FileSystemWatcher nowe wystąpienie klasy, biorąc pod uwagę określony katalog do monitorowania. |
| FileSystemWatcher(String, String) |
Inicjuje FileSystemWatcher nowe wystąpienie klasy, biorąc pod uwagę określony katalog i typ plików do monitorowania. |
FileSystemWatcher()
- Źródło:
- FileSystemWatcher.cs
- Źródło:
- FileSystemWatcher.cs
- Źródło:
- FileSystemWatcher.cs
- Źródło:
- FileSystemWatcher.cs
- Źródło:
- FileSystemWatcher.cs
Inicjuje nowe wystąpienie klasy FileSystemWatcher.
public:
FileSystemWatcher();
public FileSystemWatcher();
Public Sub New ()
Przykłady
Poniższy przykład tworzy obiekt do FileSystemWatcher obejrzenia katalogu określonego w czasie wykonywania. Obiekt FileSystemWatcher wyszukuje zmiany w LastWrite czasie i LastAccess oraz na potrzeby tworzenia, usuwania lub zmieniania nazw plików tekstowych w katalogu. Jeśli plik zostanie zmieniony, utworzony lub usunięty, ścieżka do pliku zostanie wyświetlona w konsoli programu . Po zmianie nazwy pliku do konsoli są wyświetlane stare i nowe ścieżki.
W tym przykładzie użyto System.Diagnostics przestrzeni nazw i 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
Uwagi
Nie można obserwować komputera zdalnego, który nie ma systemu Windows NT lub Windows 2000. Nie można obserwować zdalnego komputera z systemem Windows NT 4.0 z komputera z systemem Windows NT 4.0.
W poniższej tabeli przedstawiono początkowe wartości właściwości dla wystąpienia FileSystemWatcherklasy .
| Majątek | Wartość początkowa |
|---|---|
| NotifyFilter | bitowe OR kombinacja LastWrite, FileNamei DirectoryName |
| EnableRaisingEvents | false |
| Filter | "*.*" (Oglądaj wszystkie pliki). |
| IncludeSubdirectories | false |
| InternalBufferSize | 8192 |
| Path | pusty ciąg ("") |
Uwaga / Notatka
Składnik nie będzie obserwować określonego katalogu, dopóki Path parametr nie zostanie ustawiony i EnableRaisingEvents ma wartość true.
Zobacz też
- NotifyFilters
- FileSystemEventArgs
- FileSystemEventHandler
- InternalBufferOverflowException
- Path
- RenamedEventArgs
- RenamedEventHandler
- WaitForChangedResult
- WatcherChangeTypes
Dotyczy
FileSystemWatcher(String)
- Źródło:
- FileSystemWatcher.cs
- Źródło:
- FileSystemWatcher.cs
- Źródło:
- FileSystemWatcher.cs
- Źródło:
- FileSystemWatcher.cs
- Źródło:
- FileSystemWatcher.cs
Inicjuje FileSystemWatcher nowe wystąpienie klasy, biorąc pod uwagę określony katalog do monitorowania.
public:
FileSystemWatcher(System::String ^ path);
public FileSystemWatcher(string path);
new System.IO.FileSystemWatcher : string -> System.IO.FileSystemWatcher
Public Sub New (path As String)
Parametry
- path
- String
Katalog do monitorowania, w standardowej lub uniwersalnej konwencji nazewnictwa (UNC) notacji.
Wyjątki
Parametr path jest null.
Parametr path jest pustym ciągiem ("").
— lub —
Ścieżka określona za pośrednictwem parametru path nie istnieje.
path jest za długa.
Uwagi
Uwaga / Notatka
Składnik nie będzie obserwować określonego katalogu, dopóki Path parametr nie zostanie ustawiony i EnableRaisingEvents ma wartość true.
Składnik może oglądać pliki na komputerze osobistym, dysku sieciowym lub komputerze zdalnym.
Nie można obserwować komputera zdalnego, który nie ma systemu Windows NT lub Windows 2000. Nie można obserwować zdalnego komputera z systemem Windows NT 4.0 z komputera z systemem Windows NT 4.0. Właściwość Filter jest domyślnie ustawiana do oglądania wszystkich plików.
Zobacz też
- NotifyFilters
- FileSystemEventArgs
- FileSystemEventHandler
- Filter
- InternalBufferOverflowException
- Path
- RenamedEventArgs
- RenamedEventHandler
- WaitForChangedResult
- WatcherChangeTypes
Dotyczy
FileSystemWatcher(String, String)
- Źródło:
- FileSystemWatcher.cs
- Źródło:
- FileSystemWatcher.cs
- Źródło:
- FileSystemWatcher.cs
- Źródło:
- FileSystemWatcher.cs
- Źródło:
- FileSystemWatcher.cs
Inicjuje FileSystemWatcher nowe wystąpienie klasy, biorąc pod uwagę określony katalog i typ plików do monitorowania.
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)
Parametry
- path
- String
Katalog do monitorowania, w standardowej lub uniwersalnej konwencji nazewnictwa (UNC) notacji.
- filter
- String
Typ plików do obejrzenia. Na przykład wyrażenie "*.txt" obserwuje zmiany we wszystkich plikach tekstowych.
Wyjątki
Parametr path jest pustym ciągiem ("").
— lub —
Ścieżka określona za pośrednictwem parametru path nie istnieje.
path jest za długa.
Uwagi
Uwaga / Notatka
Składnik nie będzie obserwować określonego katalogu, dopóki Path parametr nie zostanie ustawiony i EnableRaisingEvents ma wartość true.
Składnik może oglądać pliki na komputerze osobistym, dysku sieciowym lub komputerze zdalnym.
Nie można obserwować komputera zdalnego, który nie ma systemu Windows NT lub Windows 2000. Nie można obserwować zdalnego komputera z systemem Windows NT 4.0 z komputera z systemem Windows NT 4.0.
Zobacz też
- NotifyFilters
- FileSystemEventArgs
- FileSystemEventHandler
- Filter
- InternalBufferOverflowException
- Path
- RenamedEventArgs
- RenamedEventHandler
- WaitForChangedResult
- WatcherChangeTypes