2010-07-15 6 views
3

나는 (나에 의해 쓰여지지 않은) 별도의 프로그램에 의한 파일 변경을 검사하는 응용 프로그램을 개발 중이다.파일을 읽을 수 있습니까?

변경 사항이 감지되면 파일을 열고 마지막 줄을 읽은 다음 파일을 닫습니다. , 난이에도 불구하고,

FileStream fs = 
    new FileStream(
     _scannerFilePath, 
     FileMode.Open, 
     FileAccess.Read, 
     FileShare.ReadWrite); 
StreamReader sr = new StreamReader(fs); 
var str = sr.ReadToEnd(); 
sr.Close(); 
fs.Close(); 

불행하게도 :

내 프로그램이 파일을 잠글하려고 노력하지만, 읽기 전용 모드에서 엽니 다하지 않도록하려면 다음 코드를 사용하고 있습니다 내 프로그램이 파일을 읽으려고 할 때마다 여전히 다음과 같은 오류가 발생합니다.

System.IO.IOException was unhandled 
    Message="The process cannot access the file 'D:\\LSDATA\\IdText.txt' because it is being used by another process." 
    Source="mscorlib" 
    StackTrace: 
     at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
     at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) 
     at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) 
     at LiquorSafe.Verification.Main.CheckLastScannedUser(String changedFileName) 
     at LiquorSafe.Verification.Main.OnChanged(Object sender, FileSystemEventArgs e) 
     at System.IO.FileSystemWatcher.OnChanged(FileSystemEventArgs e) 
     at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action, String name) 
     at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer) 
     at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) 
    InnerException: 

가능한 이유가 있습니까?

다른 프로그램이 어떤 식 으로든 읽기 잠금 장치 파일 일 수 있습니까? 그렇기 때문에 내가 읽지 못하게 할 수도 있습니까?

답변

4

예, 독점 모드으로 파일을 열 수 있습니다. 즉, 자신보다 누구도 해당 파일을 읽거나 쓸 수 없습니다.

File.Open() 기능을 살펴보십시오. 파라미터 FileShareNone으로 설정할 수 있습니다.

관련 문제