c# – 当’用户映射部分打开的文件无法执行请求的操作时出错了什么?’

我正在开发一个应用程序,它正在读取和写入很多(但同步)磁盘上的某个文件.但是,文件越大,我就越容易得到以下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,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,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中抛出异常.我怀疑异常与同时读写有关,因此试图限制对文件的访问.锁和互斥锁似乎都增加了异常的数量,而不是阻止它们.那到底是怎么回事?这个异常究竟意味着什么?如何防止它?

解决方法

我正在写一个文件(打开,循环(写入),关闭)但是在错误之后复制了对rutine的调用…所以我最终得到“所请求的操作无法在具有用户映射的文件上执行部分开放.“! 好吧,我删除了第二个电话,然后问题解决了. 似乎两个(打开/关闭)(打开/关闭)rutines在同一个文件一个一个地发生太快导致这个…… 一些开发人员建议打电话给gc. 检查每次I / O操作是否正确关闭. 不要太快执行完整(打开,关闭).似乎当一个操作将在第二个请求到达时完成并且抛出问题.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...