Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The behavior of File.Replace on Unix-based operating systems has changed. The exceptions it throws now match those that are thrown by the Windows implementation.
Previous behavior
On Unix, with .NET 5, the File.Replace method:
- Throws IOException with the message
Is a directorywhensourceFileNameis a file anddestinationFileNameis a directory. - Throws IOException with the message
Not a directorywhensourceFileNameis a directory anddestinationFileNameis a file. - Silently succeeds when both
sourceFileNameanddestinationFileNamepoint to the same file or directory.
New behavior
On Unix, with .NET 6, the File.Replace method:
- Throws UnauthorizedAccessException with the message
The specified path <path> is not a path, when eithersourceFileNameordestinationFileNameexists and is not a file, or when bothsourceFileNameanddestinationFileNamepoint to the same existing directory. - Throws IOException with the message
The source <sourceFileName> and destination <destinationFileName> are the same filewhensourceFileNameanddestinationFileNamepoint to the same existing file.
Version introduced
.NET 6
Type of breaking change
This change can affect source compatibility.
Reason for change
This change was made to ensure that File.Replace throws the same exceptions for the same reasons across platforms.
Recommended action
If you invoke File.Replace on Unix inside a try catch block, make sure to now also catch UnauthorizedAccessException. Also, be aware of the new behaviors that are caught.