FileSystemWatcher コンストラクター

定義

FileSystemWatcher クラスの新しいインスタンスを初期化します。

オーバーロード

FileSystemWatcher()

FileSystemWatcher クラスの新しいインスタンスを初期化します。

FileSystemWatcher(String)

監視するディレクトリを指定して、FileSystemWatcher クラスの新しいインスタンスを初期化します。

FileSystemWatcher(String, String)

FileSystemWatcher クラスの新しいインスタンスを、監視するディレクトリとファイルの種類を指定して初期化します。

FileSystemWatcher()

FileSystemWatcher クラスの新しいインスタンスを初期化します。

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

次の例では、 FileSystemWatcher 実行時に指定されたディレクトリを監視するオブジェクトを作成します。 オブジェクトは FileSystemWatcher 、ディレクトリ内のテキスト ファイルの LastWrite 変更と LastAccess 時刻の変更、および作成、削除、または名前変更を監視します。 ファイルが変更、作成、または削除されると、ファイルへのパスがコンソールに表示されます。 ファイルの名前が変更されると、古いパスと新しいパスがコンソールに表示されます。

この例では、名前空間と名前空間System.IOSystem.Diagnostics使用します。

#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

注釈

Windows NTまたは Windows 2000 を持たないリモート コンピューターを監視することはできません。 Windows NT 4.0 コンピューターからリモート Windows NT 4.0 コンピューターを監視することはできません。

次の表に、インスタンスの FileSystemWatcher初期プロパティ値を示します。

プロパティ 初期値
NotifyFilter FileNameビットごとの OR の組み合わせLastWrite、およびDirectoryName
EnableRaisingEvents false
Filter "*.*"(すべてのファイルを監視します)。)
IncludeSubdirectories false
InternalBufferSize 8192
Path 空の文字列 ("")

注意

コンポーネントは、設定されるまで指定されたディレクトリを Path EnableRaisingEvents true監視しません。

こちらもご覧ください

適用対象

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 (Universal Naming Convention) 表記での監視するディレクトリ。

例外

path パラメーターが null です。

path パラメーターが空の文字列 ("") です。

  • または - path パラメーターで指定されたパスが存在しません。

path が長すぎます。

注釈

注意

コンポーネントは、設定されるまで指定されたディレクトリを Path EnableRaisingEvents true監視しません。

コンポーネントは、パーソナル コンピューター、ネットワーク ドライブ、またはリモート コンピューター上のファイルを監視できます。

Windows NTまたは Windows 2000 を持たないリモート コンピューターを監視することはできません。 Windows NT 4.0 コンピューターからリモート Windows NT 4.0 コンピューターを監視することはできません。 このプロパティは Filter 、既定ですべてのファイルを監視するように設定されています。

こちらもご覧ください

適用対象

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 (Universal Naming Convention) 表記での監視するディレクトリ。

filter
String

ウォッチするファイルの種類。 たとえば、すべてのテキスト ファイルの変更をウォッチするには、"*.txt" に設定します。

例外

path パラメーターが null です。

または filter パラメーターが null です。

path パラメーターが空の文字列 ("") です。

  • または - path パラメーターで指定されたパスが存在しません。

path が長すぎます。

注釈

注意

コンポーネントは、設定されるまで指定されたディレクトリを Path EnableRaisingEvents true監視しません。

コンポーネントは、パーソナル コンピューター、ネットワーク ドライブ、またはリモート コンピューター上のファイルを監視できます。

Windows NTまたは Windows 2000 を持たないリモート コンピューターを監視することはできません。 Windows NT 4.0 コンピューターからリモート Windows NT 4.0 コンピューターを監視することはできません。

こちらもご覧ください

適用対象