2013-08-14 3 views
-1

다음 프로세스는 파일이있는 동안 대기 상태를 유지합니다. 나는 아마 오류를 만들었지 만, 나는 어디서 보지 못합니다.FileSystemWatcher Waiting

System.IO.WaitForChangedResult result; 
seeTransFile.WaitForChanged(System.IO.WatcherChangeTypes.Created); 
string seeDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
System.IO.FileSystemWatcher watcher = new FileSystemWatcher(SynDir, fileName + @".md5"); 
result = watcher.WaitForChanged(System.IO.WatcherChangeTypes.Created); 

도와 주시겠습니까?

+0

어떤 라인 프로그램이 대기합니까? 'seeTransFile'은 무엇입니까? – Damith

+0

"FileSystemWatcehr"컴포지션 내 양식에 추가하십시오 – Atoxis

+0

"seeTransFile.WaitForChanged"및 "string seeDirectory ="줄을 제공하는 이유는 무엇입니까? 샘플과 어떻게 관련이 있습니까? –

답변

1

FileSystemWatcher는 파일을 만들거나 변경할 때만 트리거됩니다. 기존 파일은 무시됩니다.

먼저 FileSystemWatcher를 시작한 다음 Directory.GetFiles를 사용하여 기존 파일을 가져옵니다. 잠금이있는 경우

try 
{ 
    // Attempts to open then close the file in RW mode, denying other users to place any locks. 
    FileStream fs = File.Open(filepath, FileMode.Open, FileAccess.ReadWrite, FileShare.None); 
    fs.Close(); 
    fileClosed = true; // success 
} 
catch (IOException) {} 

:

당신은 FileSystemWatcher에 의해 감지 된 파일의 내용을 읽어해야하는 경우

, 나는이 코드를 사용하여 파일의 소유자가 모든 잠금을 해제되었는지 확인하는 것이 좋습니다 파일에서 소유자가 파일을 릴리스 할 때까지 기다리거나 파일을 큐에 넣은 다음 나중에 다시 시도하십시오.

+0

정확히, 그 코드가 오랫동안 저에게 효과적이었습니다. 파일을 대기열에 넣고 주기적으로 검사를 마쳤습니다. 그러나 시스템이 다운되면 파일 시스템 변경 사항을 느슨하게 할 수 있습니다. 이미 처리 된 파일을 어떻게 든 표시하는 데 유용합니다. – mikus

+0

파일을 처리 한 후 하위 디렉토리로 이동하고 며칠 후에 파일을 보관/삭제합니다. 이것은 내가 Windows 탐색기에서 드래그 앤 드롭을 사용하여 파일의 배치를 다시 보낼 수 있습니다. – EventHorizon

+0

글쎄, FileWatcher evet fire 후에 변경된 파일을 등록했지만 시스템이 죽은 순간에도 여전히 잠겨 있다면 좋은 옵션이 될 것입니다. 그것의 완료 및 유일한 포인터는 메모리에 대기열입니다. – mikus

0

FileSystemWatcher 구성 요소를 이미 추가 했으므로 다음과 같이 구성하십시오. 다른 코드 줄을 제거하십시오

System.IO.WaitForChangedResult result; 
seeTransFile.Path = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
seeTransFile.Filter = fileName + @".md5"; 
result = seeTransFile.WaitForChanged(System.IO.WatcherChangeTypes.Created);