Skip to content

Commit c2805be

Browse files
Copy with Overwrite is a safer suggestion (#4295)
Added in advice to use Copy with Overwrite in all .Net versions then Delete, rather than Deleting the destination file then Moving. (Ignore the other patch. I mucked it up.)
1 parent 7f46dbf commit c2805be

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

xml/System.IO/File.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2502,6 +2502,8 @@ Note that if you attempt to replace a file by moving a file of the same name int
25022502
25032503
- In .NET Core 3.0 and later versions, you can call <xref:System.IO.File.Move(System.String,System.String,System.Boolean)> setting the parameter `overwrite` to `true`, which will replace the file if it exists.
25042504
2505+
- In all .NET versions, you can call <xref:System.IO.File.Copy(System.String, System.String, System.Boolean)> to copy with overwrite, then call `Delete` to remove the excess source file. This strategy is advisable if the file being copied is small, and you are looking for an "atomic" file operation. If you `Delete` the file first, and the system or program crashes, the destination file will no longer exist.
2506+
25052507
- In all .NET versions, you can call <xref:System.IO.File.Delete(System.String)> before calling `Move`, which will only delete the file if it exists.
25062508
25072509
The `sourceFileName` and `destFileName` arguments can include relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see <xref:System.IO.Directory.GetCurrentDirectory%2A>.

0 commit comments

Comments
 (0)