반응형
파일 변경시 알림?
디스크에서 파일이 수정 될 때 알림을받을 수있는 메커니즘 (C #)이 있습니까?
그것은 System.IO.FileSystemWatcher 입니다.
FileSystemWatcher
수업을 사용할 수 있습니다 .
public void CreateFileWatcher(string path)
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
/* 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;
}
// 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);
}
를 사용합니다 FileSystemWatcher
. 수정 이벤트에 대해서만 필터링 할 수 있습니다.
참고 URL : https://stackoverflow.com/questions/721714/notification-when-a-file-changes
반응형
'development' 카테고리의 다른 글
ggplot2에서 산점도 행렬 (pairs () 등가물) 만들기 (0) | 2020.08.13 |
---|---|
Entity Framework 코드 첫 번째 고유 열 (0) | 2020.08.13 |
MySQL : 가져올 때 오류를 무시 하시겠습니까? (0) | 2020.08.13 |
작은 따옴표 나 큰 따옴표로 묶지 않은 경우 공백을 사용하여 문자열을 분할하는 정규식 (0) | 2020.08.13 |
너비가 100 % 인 HTML 입력 텍스트 상자가 표 셀을 오버플로합니다. (0) | 2020.08.13 |