Ich entwickle eine Anwendung, die viel (aber synchron) zu einer bestimmten Datei auf der Festplatte liest und schreibt. Wie aber größer die Datei wird, desto öfter bekomme ich die folgende IOException:Was ist falsch, wenn die angeforderte Operation für eine Datei mit einem benutzerdefinierten Bereich nicht ausgeführt werden kann?
Der angeforderte Vorgang kann nicht sein auf eine Datei ausgeführt mit einem benutzer abgebildet Abschnitt offen.
auftretende aus:
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()
Wie Sie sehen können, wird die Ausnahme von innen File.WriteAllText
geworfen. Ich vermutete, dass die Ausnahme mit gleichzeitigem Lesen und Schreiben zu tun hatte, also versuchte ich, den Zugriff auf die Datei zu beschränken. Sowohl ein lock
als auch ein Mutex
schienen die Anzahl der Ausnahmen zu erhöhen, anstatt sie zu verhindern. Also, was ist genau los? Was bedeutet diese Ausnahme genau und wie kann sie verhindert werden?
Ich denke, das war auch der Fall in meiner Situation. Nicht sicher, wo die Ursache des Problems genau ist und wie das Problem verhindert werden sollte. – dbaw