IT/컴퓨터프로그램

[C#] FileSystemWatcher 풀더 감시

chn1002 2022. 12. 28. 10:57

파일 시스템 변경 알림을 수신하면서 디렉터리 또는 디렉터리의 파일이 변경되면 이벤트를 발생시킵니다.

 
public class FileSystemWatcher : System.ComponentModel.Component, System.ComponentModel.ISupportInitialize

 

FileSystemWatcher 를 사용하면 디렉토리의 변경을 알수 있다.

초기화 

    private FileSystemWatcher watcher;
    
	public void init()
    {
        watcher = new FileSystemWatcher();
        watcher.Path = galleryDirectry; 
        watcher.NotifyFilter = NotifyFilters.FileName |
                                     NotifyFilters.DirectoryName |
                                     NotifyFilters.Size |
                                     NotifyFilters.LastAccess |
                                     NotifyFilters.CreationTime |
                                     NotifyFilters.LastWrite;

		// Filter 등록: JPG 파일만 확인
        watcher.Filter = "*.jpg";
        watcher.IncludeSubdirectories = true;


		// 이벤트 핸들러 등록
        watcher.Created += new FileSystemEventHandler(Changed);
        watcher.Changed += new FileSystemEventHandler(Changed);
        watcher.Renamed += new RenamedEventHandler(Renamed);

        watcher.EnableRaisingEvents = true; 
}

 

이벤트 처리 처리된 이벤트 내용
OnChanged Changed, Created, Deleted 파일 특성, 생성된 파일 및 삭제된 파일의 변경 내용을 보고합니다.
OnRenamed Renamed 이름이 바뀐 파일 및 폴더의 이전 경로와 새 경로를 나열하고 필요한 경

 

 

파일 변경 및 생성 이벤트 handler 

    private void Renamed(object sender, RenamedEventArgs e)
    {
        throw new NotImplementedException();
    }

    private void Changed(object sender, FileSystemEventArgs e)
    {
        throw new NotImplementedException();
    }

 

Reference

- https://learn.microsoft.com/ko-kr/dotnet/api/system.io.filesystemwatcher?view=netcore-3.1 

 

FileSystemWatcher 클래스 (System.IO)

파일 시스템 변경 알림을 수신하면서 디렉터리 또는 디렉터리의 파일이 변경되면 이벤트를 발생시킵니다.

learn.microsoft.com