2014-07-05 3 views
1

나는 여러 폴더를 모니터링하기 위해 FileSystemWatcher을 사용하고 있습니다. 변경 이벤트가 발생하면 변경된 파일의 파일 이름을 가져 오려고합니다. e.Name 또는 e.FullPath를 사용할 때 감시자가 폴더를 모니터링하므로 폴더 경로가 표시됩니다.폴더의 파일 시스템 감시자이며 파일 이름을 얻는 중

파일 이름을 가져 오는 방법이 있습니까?

코드 : 그것의와 쳐들의 배열.

watchers[_Idx] = new FileSystemWatcher(); 
watchers[_Idx].Path = row.Cells[0].Value.ToString(); 
watchers[_Idx].IncludeSubdirectories = true; 
watchers[_Idx].NotifyFilter = NotifyFilters.LastWrite | 
          NotifyFilters.DirectoryName | NotifyFilters.FileName | 
          NotifyFilters.Size; 
watchers[_Idx].Changed += new FileSystemEventHandler(SyncThread); 
watchers[_Idx].Created += new FileSystemEventHandler(SyncThread); 
watchers[_Idx].Renamed +=new RenamedEventHandler(SyncThread); 
watchers[_Idx].EnableRaisingEvents = true; 
+0

FileSystemWatcher 및 해당 속성을 초기화하는 코드를 표시하십시오. – Steve

+1

디렉토리 변경 사항을 알지 않으려면 NotifyFilters.DirectoryName –

답변

1

당신은 이벤트에서 파일 이름을 추출 할 수 있습니다 :

// 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); 
} 

코드는 here에서 가져온 스 니펫.

+1

을 제거하십시오. 이전에 시도했지만 폴더 이름을 계속 가져 왔습니다. 처음에는 모든 폴더와 하위 폴더에 대한 이벤트를 제공 한 후 파일로 이동했습니다. 감사 – AshChlor

관련 문제