NLog에서 내 프로세스가 로그 파일을 읽는 동안 로그 파일을 읽는 파일 공유 문제가 있습니다. 문제를 진단 할 때 놀라운 것을 발견했습니다. 다음은 실패파일 공유가 예상대로 작동하지 않습니다.
System.IO.IOException was unhandled
Message=The process cannot access the file 'c:\...\test.file' 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, Boolean useLongPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
이 처음 FileStream
독서를 공유하는 의지를 나타낸다는 사실에도 불구하고 :
using (var fileStream1 = new FileStream("test.file", FileMode.Append, FileAccess.Write, FileShare.Read))
using (var fileStream2 = new FileStream("test.file", FileMode.Open, FileAccess.Read, FileShare.Read))
{
}
두 번째 FileStream
생성자 호출은 실패합니다.
using (var fileStream1 = new FileStream("test.file", FileMode.Append, FileAccess.Write, FileShare.Read))
using (var fileStream2 = new FileStream("test.file", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
}
음, 그래, 두 번째 스트림이 실제로 문제를 우회 열 때 더 액세스를 요청 : 내가 더 놀라운 발견 한 것은이 작품이었다. 나는 그것이 왜 그런지에 대해 완전히 당혹스럽고, 단지 내가 오해하고 있다고 가정 할 수 있습니다. 필자는 API 문서를 읽었지만 어떻게 작동해야하는지에 대한 현재의 정신 모델을 지원합니다.
열거의 전형적인 사용 두 프로세스가 동시에 동일한 파일로부터 판독 할 수 있는지 여부를 정의 할 수있다 :
여기에 docs 일부지지 인용된다. 예를 들어 파일이 이고 읽음이 지정된 경우 다른 사용자는 이라는 파일을 열어 볼 수는 있지만 쓰기는 할 수 없습니다.
는 여기에 또 다른 보석이다 :
사람은이 동작에 어떤 빛을 창고 수 있습니다. 나는 이것을 .NET 4 % Windows XP에서 테스트하고 있습니다.다음 파일 스트림 생성자는 기존 파일과 보조금을 다른 사용자 (읽기)에 는 읽기 전용 액세스를 엽니 다.
FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);
감사합니다. 따라서 오해가 너무 커서 문서가 잘못되었거나 오도 된 것일 수 있습니다. –
두 번째 스트림에는'FileShare.Write'가 포함됩니다. – rookie1024