c# - 'the requested operation cannot be performed on a file with a user-mapped section open?' 时出了什么问题

标签 c# exception file-io

我正在开发一个应用程序,它对磁盘上的某个文件进行大量读写操作(但同步进行)。但是,文件越大,我就越经常收到以下 IOException:

The requested operation cannot be performed on a file with a user-mapped section open.

发生于:

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()

如您所见,异常是从 File.WriteAllText 中抛出的。我怀疑异常与同时读写有关,因此试图限制对该文件的访问。 lockMutex 似乎都增加了异常的数量而不是阻止它们。那么到底发生了什么?这个异常到底是什么意思,如何避免?

最佳答案

第一个解决方案是关闭防病毒软件。更好的是,您可以将该文件夹添加到防病毒软件的扫描异常(exception)列表中。

关于c# - 'the requested operation cannot be performed on a file with a user-mapped section open?' 时出了什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1047616/

相关文章:

c# - ef 4.1 LINQ 将元素过滤到集合中

python - 在python中,调用后是否有可能发生异常,但其后的try block 为 "before"?

java - 异常与异常层次结构中的错误代码

c++ - Hippomock 的 MockRepository::BasicRegisterExpect 中的 NotImplementedException

algorithm - 如何计算文件的熵?

c++ - 通过套接字发送二进制文件。文本文件有效,没有别的吗?

c# - C++ 回调函数中的内存泄漏

c# - 如何使用 Linq to SQL 检索存储过程架构?

java - 如果文件已重命名,如何跟踪?

c# - SQL插入一行或多行数据?