Is XMLDocument.Save an atomic operation?

后端 未结 4 959
傲寒
傲寒 2021-01-12 08:58

Is there anyway another process monitoring for files created using XMLDocument.Save() could encounter a partial file? Does it make any difference if Save() is overwriting an

4条回答
  •  天命终不由人
    2021-01-12 09:28

    This answer https://stackoverflow.com/a/487198/1429390 provides AFAIK some kind of atomicity in an easy-to-use fashion. The principle is to write in a temporary file and provide caller an opportunity to rename the file (and whatever else you want) at close time. That way, whatever may happen while creating and filling the file cannot clobber a possibly existing file.

    Update: except it doesn't because System.IO.File.Move() refuses to overwrite. See https://stackoverflow.com/a/10305475/1429390 for a workaround.

提交回复
热议问题