FileSystemWatcher 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 FileSystemWatcher 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| FileSystemWatcher() |
初始化 FileSystemWatcher 類別的新執行個體。 |
| FileSystemWatcher(String) |
初始化該類別的新實例 FileSystemWatcher ,給定指定的監控目錄。 |
| FileSystemWatcher(String, String) |
初始化該類別的新實例 FileSystemWatcher ,根據指定的目錄與監控檔案類型。 |
FileSystemWatcher()
初始化 FileSystemWatcher 類別的新執行個體。
public:
FileSystemWatcher();
public FileSystemWatcher();
Public Sub New ()
範例
以下範例建立一個 FileSystemWatcher 物件來監控執行時指定的目錄。 物件FileSystemWatcher會監控目錄中LastWriteLastAccess文字檔的建立、刪除或重新命名,以及時間的變化。 如果檔案被更改、建立或刪除,該檔案的路徑會顯示到主控台。 當檔案被重新命名時,舊路徑和新路徑會顯示到主控台。
此範例使用 System.Diagnostics 了 and 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
備註
你無法觀看沒有 Windows NT 或 Windows 2000 的遠端電腦。 你無法從 Windows NT 4.0 電腦觀看遠端的 Windows NT 4.0 電腦。
下表顯示了 的 FileSystemWatcher初始屬性值。
| 房產 | 初始值 |
|---|---|
| NotifyFilter | 位元或組合 LastWrite、 FileName、 和 DirectoryName |
| EnableRaisingEvents | false |
| Filter | 「*.*」(觀看所有檔案。) |
| IncludeSubdirectories | false |
| InternalBufferSize | 8192 |
| Path | 空字串(“” |
備註
元件不會PathEnableRaisingEvents在設定 且 為 true之前監控指定的目錄。
另請參閱
- NotifyFilters
- FileSystemEventArgs
- FileSystemEventHandler
- InternalBufferOverflowException
- Path
- RenamedEventArgs
- RenamedEventHandler
- WaitForChangedResult
- WatcherChangeTypes
適用於
FileSystemWatcher(String)
初始化該類別的新實例 FileSystemWatcher ,給定指定的監控目錄。
public:
FileSystemWatcher(System::String ^ path);
public FileSystemWatcher(string path);
new System.IO.FileSystemWatcher : string -> System.IO.FileSystemWatcher
Public Sub New (path As String)
參數
- path
- String
監控目錄,使用標準或通用命名慣例(UNC)符號。
例外狀況
參數 path 為 null。
path 太長了。
備註
備註
元件不會PathEnableRaisingEvents在設定 且 為 true之前監控指定的目錄。
元件可以監控你個人電腦、網路磁碟機或遠端電腦上的檔案。
你無法觀看沒有 Windows NT 或 Windows 2000 的遠端電腦。 你無法從 Windows NT 4.0 電腦觀看遠端的 Windows NT 4.0 電腦。 這個 Filter 屬性預設是監控所有檔案。
另請參閱
- NotifyFilters
- FileSystemEventArgs
- FileSystemEventHandler
- Filter
- InternalBufferOverflowException
- Path
- RenamedEventArgs
- RenamedEventHandler
- WaitForChangedResult
- WatcherChangeTypes
適用於
FileSystemWatcher(String, String)
初始化該類別的新實例 FileSystemWatcher ,根據指定的目錄與監控檔案類型。
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)
參數
- path
- String
監控目錄,使用標準或通用命名慣例(UNC)符號。
- filter
- String
該看什麼類型的檔案。 例如,「*.txt」會監控所有文字檔的變更。
例外狀況
path 太長了。
備註
備註
元件不會PathEnableRaisingEvents在設定 且 為 true之前監控指定的目錄。
元件可以監控你個人電腦、網路磁碟機或遠端電腦上的檔案。
你無法觀看沒有 Windows NT 或 Windows 2000 的遠端電腦。 你無法從 Windows NT 4.0 電腦觀看遠端的 Windows NT 4.0 電腦。
另請參閱
- NotifyFilters
- FileSystemEventArgs
- FileSystemEventHandler
- Filter
- InternalBufferOverflowException
- Path
- RenamedEventArgs
- RenamedEventHandler
- WaitForChangedResult
- WatcherChangeTypes