我正在开发一个应用程序,它正在读取和写入很多(但同步)磁盘上的某个文件.但是,文件越大,我就越容易得到以下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中抛出异常.我怀疑异常与同时读写有关,因此试图限制对文件的访问.锁和互斥锁似乎都增加了异常的数量,而不是阻止它们.那到底是怎么回事?这个异常究竟意味着什么?如何防止它?
解决方法
原文地址:https://www.jb51.cc/csharp/238963.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。