파일이나 디렉토리가 삭제 된 경우 FSW를 확인하는 방법이 있습니까?.NET filesystemwatcher - 파일이나 디렉토리였습니까?
답변
FileSystemEventArgs.FullPath
속성을 조사하여 디렉토리인지 파일인지 알 수 있습니다.
if (Path.GetFileName(e.FullPath) == String.Empty)
{
//it's a directory.
}
파일 또는 디렉토리인지 확인하십시오.
namespace Watcher
{
class Program
{
private const string Directory = @"C:\Temp";
private static FileSystemWatcher _fileWatcher;
private static FileSystemWatcher _dirWatcher;
static void Main(string[] args)
{
_fileWatcher = new FileSystemWatcher(Directory);
_fileWatcher.IncludeSubdirectories = true;
_fileWatcher.NotifyFilter = NotifyFilters.FileName;
_fileWatcher.EnableRaisingEvents = true;
_fileWatcher.Deleted += WatcherActivity;
_dirWatcher = new FileSystemWatcher(Directory);
_dirWatcher.IncludeSubdirectories = true;
_dirWatcher.NotifyFilter = NotifyFilters.DirectoryName;
_dirWatcher.EnableRaisingEvents = true;
_dirWatcher.Deleted += WatcherActivity;
Console.ReadLine();
}
static void WatcherActivity(object sender, FileSystemEventArgs e)
{
if(sender == _dirWatcher)
{
Console.WriteLine("Directory:{0}",e.FullPath);
}
else
{
Console.WriteLine("File:{0}",e.FullPath);
}
}
}
}
삭제되었으므로 이러한 호출이 실패합니다. –
@Steven Sudit : 먼저 FileSystemEventArgs.ChangeType 속성을 확인해야합니다. –
'File.GetAttributes'는 삭제 된 파일에 대해'FileNotFoundException'을 던집니다. –
여기 단순화 및 수정 플레처의 솔루션의 버전입니다. 그러나 파일과 동일한 경로에서 같은 이름의 디렉토리가있을 수 있다면
가bool isDirectory = Path.GetExtension(e.FullPath) == string.Empty;
if (e.ChangeType != WatcherChangeTypes.Deleted)
{
isDirectory = Directory.Exists(e.FullPath);
}
나는 임시로 사용 초기에 "경로"기능,하지만 나중에 내가 Directory.Exists하여 수정 삭제하지 않는 경우 :
귀하의 질문은 의미가 삭제 경우가 해결되지 않습니다. 예 : 확장자가없는 파일 이름이나 확장자를 가진 디렉토리가있는 경우.
파일과 디렉토리가 존재하는지 여부에 관계없이 전체 경로 (bool iSDirectory = Path.GetExtension(e.FullPath).Equals("");
)의 확장이 있는지 확인하기 만하면됩니다.이 방법은 주어진 경로를 구문 분석하기 때문에 파일에 아무런 관련이 없습니다.
내가 처음 언급 한 비 전통적인 문제를 처리해야한다면 은 디렉토리 또는 파일이 해당 위치에 존재하는지 확인하십시오. 두 경우 모두 삭제 된 것처럼 처리합니다. 그 중 하나가 존재하면, 다른 하나는 삭제 된 것으로 간주합니다.
귀하의 문의는 귀하가 파일과 디렉토리 목록을 어딘가에 보관한다는 것을 의미하므로 해당 목록을 확인하면 처리에 대한 결정을 내릴 수 있습니다.
나는이 접근법이 두 파일 시스템 관찰자를 사용하여 차이를 알려주는 해결책보다 낫다고 생각한다.
- 1. FileSystemWatcher
- 2. FileSystemWatcher OnOpened 이벤트
- 3. FileSystemWatcher 문제
- 4. FileSystemWatcher 클래스
- 5. Samba에서 잘못된 FileSystemWatcher 동작
- 6. FileSystemWatcher 작업이 완료 되었습니까?
- 7. FileSystemWatcher 주위에 간단한 .NET 래퍼 서비스를 작성하십시오. 변경된 이벤트
- 8. FileSystemWatcher 및 타이머를 .Net 2.0 Windows 서비스에 추가
- 9. FileSystemWatcher 이벤트 트리거 문제
- 10. FileSystemWatcher IDE의 이벤트
- 11. C# FileSystemWatcher 잠금 폴더
- 12. HFS + 파일 시스템의 FileSystemWatcher
- 13. FileSystemWatcher 호출 중단 처리
- 14. FileSystemWatcher 이벤트를 어떻게 테스트합니까?
- 15. 네트워크 컴퓨터 설정에서 FileSystemWatcher
- 16. FileSystemWatcher 및 windows 7
- 17. FileSystemWatcher, 이상한 응답
- 18. FileSystemWatcher 우선 순위
- 19. DeepZoom + FileSystemWatcher 클래스
- 20. filesystemwatcher multiple files
- 21. 어떤 사용자가 FileSystemWatcher 이벤트를 일으켰습니까?
- 22. 가장 된 사용자로 실행중인 FileSystemWatcher
- 23. 대기중인 FileSystemWatcher 이벤트를 비우는 방법?
- 24. FileSystemWatcher - 대체 데이터 스트림 모니터링
- 25. FileSystemWatcher 변경된 이벤트가 변경되지 않았습니다.
- 26. 파일 스트림 열기를위한 FileSystemWatcher 트리거
- 27. 매핑 된 네트워크 드라이브의 FileSystemWatcher
- 28. 사용중인 파일이나 폴더에 대한 문제 :
- 29. 배쉬 : 해당 파일이나 디렉토리
- 30. XML 파일이나 데이터베이스를 사용해야합니까?
저는 솔직히 당신이 할 수 있을지 확신하지 못합니다. 확실한 해결 방법은 시작할 때 디렉토리 목록을 작성하기 위해 반복하는 것입니다. 그 목록에 없다면 그것은 파일이었습니다. –
"C# FileSystemWatcher"가 아닙니다. .NET FileSystemWatcher입니다. C#뿐 아니라 모든 .NET 언어에서도 작동합니다. –
두 개의 분리 된 FSW를 실행하여 동일한 디렉토리를 모니터링하고 'NotifyFiter' 값만 다른 경우 작동 할 수 있습니다. –