代码之家  ›  专栏  ›  技术社区  ›  Bryan Denny

系统。IO.Exception错误:“无法在打开用户映射节的文件上执行请求的操作。”

  •  39
  • Bryan Denny  · 技术社区  · 17 年前

    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)
    

    对发生的事情有什么想法吗?

    5 回复  |  直到 15 年前
        1
  •  43
  •   Richard    17 年前

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

        2
  •  12
  •   RichieHindle    17 年前

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

    你在编辑器中打开了这个文件吗?您是否有其他代码读取它,但忘记关闭它?

    您可以使用 Process Explorer Find Find handle or DLL...

        3
  •  7
  •   VipX1    13 年前

    执行多次写入时,锁将阻止问题。

    lock(file){ write to file code here }
    
        4
  •  5
  •   Mark Lakata    13 年前

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

    如果你必须快速写入大量文件,你可以使用Thread添加一个小延迟。Sleep()似乎也让操作系统摆脱了你的负担。

     while (i++<100000000) {
       File.WriteAllLines(...)
       Thread.Sleep(1);
     }
    
        5
  •  0
  •   er.animesh    5 年前