Alex Rivera | Logout

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

Asked 2009-08-19T21:25:50.987
39

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

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.

Any ideas on what happened?

Edit
Report

2 Answers

47

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

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

answered 2009-08-19T21:33:24.590
7

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.

A lock will stop the issue when doing multiple writes.

lock(file){ write to file code here }
answered 2012-11-15T12:30:47.493

Your Answer