C# 当“无法在用户映射部分打开的文件上执行请求的操作”时有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1047616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's wrong when 'the requested operation cannot be performed on a file with a user-mapped section open?'
提问by dbaw
I'm developing an application that is reading and writing a lot (but synchronously, though) to a certain file on disk. However, how larger the file gets, the more often I get the following IOException:
我正在开发一个应用程序,它对磁盘上的某个文件进行大量(但同步)读写。但是,文件越大,我越经常收到以下 IOException:
The requested operation cannot be performed on a file with a user-mapped section open.
无法对打开了用户映射部分的文件执行请求的操作。
occuring from:
发生于:
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()
As you can see, the exception is thrown from within File.WriteAllText
. I suspected the exception had to do with simultaneous reading and writing, so tried to limit access to the file. Both a lock
and a Mutex
seemed to increase the number of exceptions instead of preventing them. So what's exactly going on? What does this exception exactly mean and how can it be prevented?
如您所见,异常是从File.WriteAllText
. 我怀疑异常与同时读取和写入有关,因此尝试限制对文件的访问。alock
和 aMutex
似乎都增加了异常的数量,而不是阻止它们。那么究竟是怎么回事呢?这个异常究竟是什么意思,如何防止?
采纳答案by janux
I was writing to a file (open,loop(writes),close) but after a mistake a duplicated the call to the rutine... so I end up with "The requested operation cannot be performed on a file with a user-mapped section open."! Well, I deleted the second call and then the problem solved. It seems that two (open/close) (open/close) rutines over the same file happening too soon one after another causes this... Some developers suggest to call the gc. Check for closing correctly every i/o operation. Not to perform complete (open,loop(writes),close) too soon. It seems that when one operation it is going to be finished while the second request arrives and that throws the problem.
我正在写入一个文件(打开,循环(写入),关闭)但是在一个错误之后重复了对 rutine 的调用......所以我最终得到了“无法对具有用户映射的文件执行请求的操作部分打开。”!好吧,我删除了第二个电话,然后问题解决了。似乎对同一个文件的两个(打开/关闭)(打开/关闭)程序一个接一个地发生得太快导致了这个......一些开发人员建议调用gc。检查每个 I/O 操作是否正确关闭。不要过早执行完成(打开、循环(写入)、关闭)。似乎当一个操作将在第二个请求到达时完成并引发问题。
回答by Assaf Lavie
Does this still happen even with only one thread/process?
即使只有一个线程/进程,这种情况还会发生吗?
If so, it's not a threading issue.
如果是这样,那就不是线程问题。
If not, then you might not be using the mutex/lock correctly. Maybe you should post the relevant code here.
如果没有,那么您可能没有正确使用互斥锁/锁。也许您应该在此处发布相关代码。
回答by Daniel Earwicker
Do you have real time virus scanning software enabled?
您是否启用了实时病毒扫描软件?
I've lost count of the number of times file locking issues have turned out to be caused by virus scanning software.
我已经不知道有多少次文件锁定问题是由病毒扫描软件引起的。
回答by Zodman
I've seen this message when the Visual Studio debugger gets in a knot and locks the PDB file.
当 Visual Studio 调试器陷入困境并锁定 PDB 文件时,我已经看到了这条消息。
That may not be relevant to your situation, but you can confirm if the file is locked (and what by) by using a downloadable utility called "Unlocker" which can detect and remove locks on files and folders.
这可能与您的情况无关,但您可以使用名为“Unlocker”的可下载实用程序来确认文件是否被锁定(以及锁定了什么),该实用程序可以检测和删除文件和文件夹上的锁定。
回答by nwolisa
The first solution will be to turn off your antivirus. Better still you can add the folder to the scan exception list of your antivirus.
第一个解决方案是关闭您的防病毒软件。更好的是,您可以将该文件夹添加到防病毒软件的扫描例外列表中。
回答by J?rn Jensen
To me, that sounds like you have some other process that opens all modified files with permissions that mean you cannot open it for modification. This could be antivirus, online backup, online file sync...
对我来说,这听起来像是您有其他一些进程可以打开所有修改过的文件,这意味着您无法打开它进行修改。这可能是防病毒、在线备份、在线文件同步...