Estoy desarrollando una aplicación que está leyendo y escribiendo mucho (pero sincrónicamente, sin embargo) a un cierto archivo en el disco. Sin embargo, la forma más grande sea el archivo consigue, más a menudo me sale el siguiente IOException:¿Qué sucede cuando 'la operación solicitada no puede realizarse en un archivo con una sección mapeada por el usuario abierta?'
La operación solicitada no se puede realizar en un archivo con una sección asignada de usuario abierta.
occuring de:
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()
Como se puede ver, la excepción es lanzada desde el interior de File.WriteAllText
. Sospeché que la excepción tenía que ver con la lectura y la escritura simultáneas, así que traté de limitar el acceso al archivo. Tanto un lock
como un Mutex
parecían aumentar el número de excepciones en lugar de prevenirlas. Entonces, ¿qué está pasando exactamente? ¿Qué significa exactamente esta excepción y cómo se puede prevenir?
Creo que este también fue el caso en mi situación. No estoy seguro de dónde está exactamente la causa del problema y cómo debe evitarse el problema. – dbaw