FileSystemWatcher 建構函式

定義

初始化 FileSystemWatcher 類別的新執行個體。

多載

FileSystemWatcher()

初始化 FileSystemWatcher 類別的新執行個體。

FileSystemWatcher(String)

提供要監視的指定目錄,初始化 FileSystemWatcher 類別的新執行個體。

FileSystemWatcher(String, String)

提供要監視的指定目錄和檔案類型,初始化 FileSystemWatcher 類別的新執行個體。

FileSystemWatcher()

來源:
FileSystemWatcher.cs
來源:
FileSystemWatcher.cs
來源:
FileSystemWatcher.cs

初始化 FileSystemWatcher 類別的新執行個體。

public:
 FileSystemWatcher();
public FileSystemWatcher ();
Public Sub New ()

範例

下列範例會FileSystemWatcher建立 物件,以 watch 運行時間指定的目錄。 物件FileSystemWatcher會監看和時間的LastWriteLastAccess變更,以及建立、刪除或重新命名目錄中的文本檔。 如果檔案已變更、建立或刪除,檔案的路徑會顯示至主控台。 重新命名檔案時,舊的和新路徑會顯示至主控台。

這個範例會使用 System.DiagnosticsSystem.IO 命名空間。

#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

備註

您無法 watch 沒有 Windows NT 或 Windows 2000 的遠端電腦。 您無法從 Windows NT 4.0 計算機 watch 遠端 Windows NT 4.0 電腦。

下表顯示 實例 FileSystemWatcher的初始屬性值。

屬性 初始值
NotifyFilter FileName和 的LastWrite位 OR 組合DirectoryName
EnableRaisingEvents false
Filter “*.*” (監看所有 files.)
IncludeSubdirectories false
InternalBufferSize 8192
Path 空字串 (“”)

注意

在設定 之前,元件不會 watch 指定的目錄Path,而且 EnableRaisingEventstrue

另請參閱

適用於

FileSystemWatcher(String)

來源:
FileSystemWatcher.cs
來源:
FileSystemWatcher.cs
來源:
FileSystemWatcher.cs

提供要監視的指定目錄,初始化 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

要監視的目錄,使用標準或通用命名慣例標記法。

例外狀況

path 參數為 null

path 參數是空字串 ("")。

-或-

透過 path 參數指定的路徑不存在。

path 太長。

備註

注意

在設定 之前,元件不會 watch 指定的目錄Path,而且 EnableRaisingEventstrue

元件可以 watch 個人電腦上的檔案、網路驅動器機或遠端電腦。

您無法 watch 沒有 Windows NT 或 Windows 2000 的遠端電腦。 您無法從 Windows NT 4.0 計算機 watch 遠端 Windows NT 4.0 電腦。 屬性Filter預設會設定為 watch 所有檔案。

另請參閱

適用於

FileSystemWatcher(String, String)

來源:
FileSystemWatcher.cs
來源:
FileSystemWatcher.cs
來源:
FileSystemWatcher.cs

提供要監視的指定目錄和檔案類型,初始化 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

要監視的目錄,使用標準或通用命名慣例標記法。

filter
String

要監看的檔案類型。 例如,"*.txt" 監看所有文字檔的變更。

例外狀況

path 參數為 null

-或-

filter 參數為 null

path 參數是空字串 ("")。

-或-

透過 path 參數指定的路徑不存在。

path 太長。

備註

注意

在設定 之前,元件不會 watch 指定的目錄Path,而且 EnableRaisingEventstrue

元件可以 watch 個人電腦上的檔案、網路驅動器機或遠端電腦。

您無法 watch 沒有 Windows NT 或 Windows 2000 的遠端電腦。 您無法從 Windows NT 4.0 計算機 watch 遠端 Windows NT 4.0 電腦。

另請參閱

適用於