C# System.IO.Exception 错误:“无法在用户映射部分打开的文件上执行请求的操作。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1302698/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:54:19  来源:igfitidea点击:

System.IO.Exception error: "The requested operation cannot be performed on a file with a user-mapped section open."

c#xmlioexception

提问by Bryan Denny

I received a very weird IOException when writing to an XML file:

写入 XML 文件时,我收到了一个非常奇怪的 IOException:

System.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)
   at System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding)
   at System.Xml.XmlDocument.Save(String filename)

The error happened when I called XmlDocument's Save(string) function.

当我调用 XmlDocument 的 Save(string) 函数时发生错误。

Any ideas on what happened?

关于发生了什么的任何想法?

采纳答案by Richard

Looks like another process had the file open using the file mapping (shared memory) APIs.

看起来另一个进程使用文件映射(共享内存)API 打开了文件。

The find function in Process Explorer should be able to tell you.

Process Explorer 中的 find 函数应该能够告诉你。

回答by RichieHindle

It looks like the file you're trying to write is already open elsewhere, either by your code or by another process.

您尝试写入的文件似乎已在其他地方打开,无论是您的代码还是其他进程。

Do you have the file open in an editor? Do you have some other code that reads it, but forgets to close it?

你有在编辑器中打开的文件吗?您是否有其他一些代码可以读取它,但忘记关闭它?

You can use Process Explorerto find out which process has open file handle on it - use the Find/ Find handle or DLL...command.

您可以使用Process Explorer找出哪个进程具有打开的文件句柄 - 使用Find/Find handle or DLL...命令。

回答by Mark Lakata

The OS or Framework can fail you, if you try to open the same file over and over again in a tight loop, for example

例如,如果您尝试在一个紧密循环中一遍又一遍地打开同一个文件,操作系统或框架可能会让您失望

 while (true) {
   File.WriteAllLines(...)
 }

Of course, you don't want to actually do that. But a bug in your code might cause that to happen. The crash is not due to your code, but a problem with Windows or the .NET Framework.

当然,你不想真正这样做。但是您的代码中的错误可能会导致这种情况发生。崩溃不是由于您的代码,而是 Windows 或 .NET Framework 的问题。

If you have to write lots of files very quickly, you could add a small delay with Thread.Sleep() which also seems to get the OS off your back.

如果您必须非常快速地编写大量文件,您可以使用 Thread.Sleep() 添加一个小的延迟,这似乎也让操作系统摆脱了您的束缚。

 while (i++<100000000) {
   File.WriteAllLines(...)
   Thread.Sleep(1);
 }

回答by VipX1

Try excluding the file from your project while you debug. I found that it was in fact VS2010 which was holding the XML file. You can then select "Show all files" in your solution explorer to check the XML file post debug.

尝试在调试时从项目中排除该文件。我发现实际上是 VS2010 保存了 XML 文件。然后,您可以在解决方案资源管理器中选择“显示所有文件”以检查调试后的 XML 文件。

A lock will stop the issue when doing multiple writes.

进行多次写入时,锁定将停止问题。

lock(file){ write to file code here }