FileSystemWatcher 생성자

정의

FileSystemWatcher 클래스의 새 인스턴스를 초기화합니다.

오버로드

FileSystemWatcher()

FileSystemWatcher 클래스의 새 인스턴스를 초기화합니다.

FileSystemWatcher(String)

모니터링할 디렉터리가 지정된 경우 FileSystemWatcher 클래스의 새 인스턴스를 초기화합니다.

FileSystemWatcher(String, String)

모니터링할 디렉터리 및 파일 형식이 지정된 경우 FileSystemWatcher 클래스의 새 인스턴스를 초기화합니다.

FileSystemWatcher()

Source:
FileSystemWatcher.cs
Source:
FileSystemWatcher.cs
Source:
FileSystemWatcher.cs

FileSystemWatcher 클래스의 새 인스턴스를 초기화합니다.

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

예제

다음 예제에서는 런타임에 지정된 디렉터리를 watch 개체를 만듭니다FileSystemWatcher. 개체는 FileSystemWatcher 및 시간의 변경 LastWrite 내용과 LastAccess 디렉터리의 텍스트 파일 만들기, 삭제 또는 이름 바꾸기를 감시합니다. 파일이 변경, 생성 또는 삭제되면 파일 경로가 콘솔에 표시됩니다. 파일 이름을 바꾸면 이전 경로와 새 경로가 콘솔에 표시됩니다.

이 예제에서는 및 System.IO 네임스페이 System.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이 없는 원격 컴퓨터는 watch 수 없습니다. Windows NT 4.0 컴퓨터에서 원격 Windows NT 4.0 컴퓨터를 watch 수 없습니다.

다음 표에서 인스턴스에 대 한 초기 속성 값을 보여 줍니다. FileSystemWatcher합니다.

속성 초기 값
NotifyFilter , FileName및 의 LastWrite비트 OR 조합DirectoryName
EnableRaisingEvents false
Filter "*.*"(모든 파일 보기)
IncludeSubdirectories false
InternalBufferSize 8192
Path 빈 문자열("")

참고

구성 요소는 가 설정 EnableRaisingEventstrue되고 가 가 될 때까지 Path 지정된 디렉터리를 watch 않습니다.

추가 정보

적용 대상

FileSystemWatcher(String)

Source:
FileSystemWatcher.cs
Source:
FileSystemWatcher.cs
Source:
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

모니터링할 디렉터리입니다. 표준 또는 UNC(Universal Naming Convention) 표기법으로 나타냅니다.

예외

path 매개 변수가 null인 경우

path 매개 변수가 빈 문자열("")입니다.

또는

path 매개 변수로 지정된 경로가 없는 경우

path가 너무 깁니다.

설명

참고

구성 요소는 가 설정 EnableRaisingEventstrue되고 가 가 될 때까지 Path 지정된 디렉터리를 watch 않습니다.

구성 요소는 개인 컴퓨터, 네트워크 드라이브 또는 원격 컴퓨터의 파일을 watch 수 있습니다.

Windows NT 또는 Windows 2000이 없는 원격 컴퓨터는 watch 수 없습니다. Windows NT 4.0 컴퓨터에서 원격 Windows NT 4.0 컴퓨터를 watch 수 없습니다. 속성은 Filter 기본적으로 모든 파일을 watch 설정됩니다.

추가 정보

적용 대상

FileSystemWatcher(String, String)

Source:
FileSystemWatcher.cs
Source:
FileSystemWatcher.cs
Source:
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

모니터링할 디렉터리입니다. 표준 또는 UNC(Universal Naming Convention) 표기법으로 나타냅니다.

filter
String

조사할 파일 형식입니다. 예를 들어 "*.txt"를 지정하면 모든 텍스트 파일에 대한 변경 내용을 조사합니다.

예외

path 매개 변수가 null인 경우

또는

filter 매개 변수가 null인 경우

path 매개 변수가 빈 문자열("")입니다.

또는

path 매개 변수로 지정된 경로가 없는 경우

path가 너무 깁니다.

설명

참고

구성 요소는 가 설정 EnableRaisingEventstrue되고 가 가 될 때까지 Path 지정된 디렉터리를 watch 않습니다.

구성 요소는 개인 컴퓨터, 네트워크 드라이브 또는 원격 컴퓨터의 파일을 watch 수 있습니다.

Windows NT 또는 Windows 2000이 없는 원격 컴퓨터는 watch 수 없습니다. Windows NT 4.0 컴퓨터에서 원격 Windows NT 4.0 컴퓨터를 watch 수 없습니다.

추가 정보

적용 대상