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

后端 未结 5 846
梦如初夏
梦如初夏 2020-12-03 04:42

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-map         


        
5条回答
  •  臣服心动
    2020-12-03 05:15

    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.

    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.

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

提交回复
热议问题