이 문서는 기계 번역을 이용하여 번역되었습니다. 문서를 영문으로 보려면 영문 확인란을 선택하세요. 마우스 포인터를 텍스트 위로 이동시켜 팝업 창에서 영문 텍스트를 표시할 수도 있습니다.
번역
영문

FileSystemWatcher.Changed 이벤트

 

게시 날짜: 2016년 11월

지정된 Path에서 파일이나 디렉터리가 변경될 경우에 발생합니다.

네임스페이스:   System.IO
어셈블리:  System(System.dll에 있음)

[IODescriptionAttribute("FSW_Changed")]
public event FileSystemEventHandler Changed

The E:System.IO.FileSystemWatcher.Changed event is raised when changes are made to the size, system attributes, last write time, last access time, or security permissions of a file or directory in the directory being monitored.

System_CAPS_note참고

Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several M:System.IO.FileSystemWatcher.OnChanged(System.IO.FileSystemEventArgs) and some M:System.IO.FileSystemWatcher.OnCreated(System.IO.FileSystemEventArgs) and M:System.IO.FileSystemWatcher.OnDeleted(System.IO.FileSystemEventArgs) events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by T:System.IO.FileSystemWatcher.

Use P:System.IO.FileSystemWatcher.NotifyFilter to restrict the number of notifications raised when this event is handled.

System_CAPS_note참고

The E:System.IO.FileSystemWatcher.Changed event is raised unexpectedly when a file is renamed, but is not raised when a directory is renamed. To watch for renaming, use the E:System.IO.FileSystemWatcher.Renamed event.

System_CAPS_note참고

The order in which the E:System.IO.FileSystemWatcher.Changed event is raised in relation to the other T:System.IO.FileSystemWatcher events may change when the P:System.IO.FileSystemWatcher.SynchronizingObject property is not null.

The following example uses the E:System.IO.FileSystemWatcher.Changed event to display the file path to the console whenever the watched file is changed.

using System;
using System.IO;
using System.Security.Permissions;

public class Watcher
{

    public static void Main()
    {
    Run();

    }

    [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
    public static void Run()
    {
        string[] args = System.Environment.GetCommandLineArgs();

        // If a directory is not specified, exit program.
        if(args.Length != 2)
        {
            // Display the proper way to call the program.
            Console.WriteLine("Usage: Watcher.exe (directory)");
            return;
        }

        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = args[1];
        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        // Only watch text files.
        watcher.Filter = "*.txt";

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

        // Begin watching.
        watcher.EnableRaisingEvents = true;

        // Wait for the user to quit the program.
        Console.WriteLine("Press \'q\' to quit the sample.");
        while(Console.Read()!='q');
    }

    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
       Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
    }

    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        // Specify what is done when a file is renamed.
        Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
    }
}

.NET Framework
1.1 이후 사용 가능
맨 위로 이동
표시: