Tôi đang phát triển một ứng dụng đang đọc và viết rất nhiều (nhưng đồng bộ, mặc dù) vào một tệp nhất định trên đĩa. Tuy nhiên, làm thế nào lớn hơn các tập tin được, càng thường xuyên hơn tôi nhận được sau IOException:Điều gì xảy ra khi 'thao tác được yêu cầu không thể được thực hiện trên một tệp có phần do người dùng ánh xạ mở?'
Các hoạt động yêu cầu không thể được thực hiện trên một tập tin với một người dùng ánh xạ phần mở.
occuring từ:
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, Int32 bufferSize, FileOptions options)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
at System.IO.File.WriteAllText(String path, String contents, Encoding encoding)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at TestApp.Program.Main()
Như bạn có thể thấy, các ngoại lệ được ném từ bên trong File.WriteAllText
. Tôi nghi ngờ ngoại lệ phải làm với việc đọc và ghi đồng thời, do đó, cố gắng hạn chế quyền truy cập vào tệp. Cả hai số lock
và Mutex
dường như tăng số lượng ngoại lệ thay vì ngăn chặn chúng. Vì vậy, những gì chính xác đang xảy ra? Ngoại lệ này chính xác có ý nghĩa gì và nó có thể được ngăn chặn như thế nào?
Tôi nghĩ đây cũng là trường hợp trong tình huống của tôi. Không chắc chắn nguyên nhân của vấn đề chính xác là gì và vấn đề nên được ngăn chặn như thế nào. – dbaw