2013-10-16 3 views
1

새 파일이있는 폴더를 모니터링하고 있고 새 파일이 있으면 다음과 같이 파일을 읽은 다음 (txt로 저장)잘라 내기/붙여 넣기가 아닌 복사/붙여 넣기 일 경우에만 파일이 잠김

FileStream file = File.Open(this.filePath, FileMode.Open, FileAccess.Read); 
StreamReader reader = new System.IO.StreamReader(file); 
string text = reader.ReadToEnd(); 
reader.Close(); 

소스 파일의 폴더에 복사/붙여 넣기를하면 다른 프로세스에서 파일을 사용하고 있음을 알리는 IOExcpetion이 수신됩니다. 폴더에서 잘라내어 붙여 넣기하면 모두 작동합니다. 또한 잠금 문제가 발생합니다. 복사 한 경우 (이 경우에도 잘라 내기)/다른 컴퓨터의 파일을 모니터링 대상 폴더에 붙여 넣습니다.

무슨 일이 일어나고 있는지 알고 계십니까?

이러한 유형의 잠금을 피하기 위해 파일에 액세스하는 더 안전한 방법이 있습니까?

감사합니다.

+0

하기 전에이 작업을 구현할 수있다? – Anirudha

+0

FileSystemWatcher (하나 이상의 폴더를 모니터링하기 위해 래핑 클래스 내에서 사용) – ff8mania

+0

아마도 'FileSystemWatcher'와 충돌이있을 것입니다! 우리에게 코드 – Anirudha

답변

0

다음은 파일 복사가 완료되었는지 또는 다른 프로세스에서 사용 중이 아닌지 확인하기위한 간단한 스 니펫입니다.

private bool FileUploadCompleted(string filename) 
    { 
     try 
     { 
      using (FileStream inputStream = File.Open(filename, FileMode.Open, 
       FileAccess.Read, 
       FileShare.None)) 
      { 
       return true; 
      } 
     } 
     catch (IOException) 
     { 
      return false; 
     } 
    } 

그럼 당신은 어떻게 당신이 folder..Show 미 code..Are 사용은 FileSystemWatcher를 모니터링하는 프로세스 로직

while (!FileUploadCompleted(filePath)) 
{ 
    //if the file is in use it will enter here 
    //So you could sleep the thread here for a second or something to allow it some time 
    // Also you could add a retry count and if it goes past the allotted retries you 
    // can break the loop and send an email or log the file for manual processing or 
    // something like that 

} 
관련 문제