Skip to content

Commit 3d3722b

Browse files
authored
file.replace exceptions (#26505)
1 parent 3ed43f6 commit 3d3722b

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

docs/core/compatibility/6.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ If you're migrating an app to .NET 6, the breaking changes listed here might aff
4646
| [Changes to nullable reference type annotations](core-libraries/6.0/nullable-ref-type-annotation-changes.md) | ✔️ || Preview 1-2 |
4747
| [Conditional string evaluation in Debug methods](core-libraries/6.0/debug-assert-conditional-evaluation.md) | ✔️ || RC 1 |
4848
| [Environment.ProcessorCount behavior on Windows](core-libraries/6.0/environment-processorcount-on-windows.md) | ✔️ || Preview 2 |
49+
| [File.Replace on Unix throws exceptions to match Windows](core-libraries/6.0/file-replace-exceptions-on-unix.md) | ✔️ || Preview 7 |
4950
| [FileStream no longer synchronizes file offset with OS](core-libraries/6.0/filestream-doesnt-sync-offset-with-os.md) ||| Preview 4 |
5051
| [FileStream.Position updates after ReadAsync or WriteAsync completes](core-libraries/6.0/filestream-position-updates-after-readasync-writeasync-completion.md) ||| Preview 4 |
5152
| [New diagnostic IDs for obsoleted APIs](core-libraries/6.0/diagnostic-id-change-for-obsoletions.md) | ✔️ || Preview 5 |
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: ".NET 6 breaking change: File.Replace exceptions on Unix"
3+
description: Learn about the .NET 6 breaking change in core .NET libraries where File.Replace throws exceptions in more cases on Unix-based operating systems.
4+
ms.date: 10/13/2021
5+
---
6+
# File.Replace on Unix throws exceptions to match Windows implementation
7+
8+
The behavior of <xref:System.IO.File.Replace%2A?displayProperty=nameWithType> on Unix-based operating systems has changed. The exceptions it throws now match those that are thrown by the Windows implementation.
9+
10+
## Previous Behavior
11+
12+
On Unix, with .NET 5, the <xref:System.IO.File.Replace%2A?displayProperty=nameWithType> method:
13+
14+
- Throws <xref:System.IO.IOException> with the message `Is a directory` when `sourceFileName` is a file and `destinationFileName` is a directory.
15+
- Throws <xref:System.IO.IOException> with the message `Not a directory` when `sourceFileName` is a directory and `destinationFileName` is a file.
16+
- Silently succeeds when both `sourceFileName` and `destinationFileName` point to the same file or directory.
17+
18+
## New behavior
19+
20+
On Unix, with .NET 6, the <xref:System.IO.File.Replace%2A?displayProperty=nameWithType> method:
21+
22+
- Throws <xref:System.UnauthorizedAccessException> with the message `The specified path <path> is not a path`, when either `sourceFileName` or `destinationFileName` exists and is not a file, or when both `sourceFileName` and `destinationFileName` point to the same existing directory.
23+
- Throws <xref:System.IO.IOException> with the message `The source <sourceFileName> and destination <destinationFileName> are the same file` when `sourceFileName` and `destinationFileName` point to the same existing file.
24+
25+
## Version introduced
26+
27+
6.0 Preview 7
28+
29+
## Type of breaking change
30+
31+
This change can affect [source compatibility](../../categories.md#source-compatibility).
32+
33+
## Reason for change
34+
35+
This change was made to ensure that <xref:System.IO.File.Replace%2A?displayProperty=nameWithType> throws the same exceptions for the same reasons across platforms.
36+
37+
## Recommended action
38+
39+
If you invoke <xref:System.IO.File.Replace%2A?displayProperty=nameWithType> on Unix inside a `try catch` block, make sure to now also catch <xref:System.UnauthorizedAccessException>. Also, be aware of the new behaviors that are caught.
40+
41+
## Affected APIs
42+
43+
- <xref:System.IO.File.Replace%2A?displayProperty=fullName>

docs/core/compatibility/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ items:
7575
href: core-libraries/6.0/debug-assert-conditional-evaluation.md
7676
- name: Environment.ProcessorCount behavior on Windows
7777
href: core-libraries/6.0/environment-processorcount-on-windows.md
78+
- name: File.Replace on Unix throws exceptions to match Windows
79+
href: core-libraries/6.0/file-replace-exceptions-on-unix.md
7880
- name: FileStream no longer synchronizes offset with OS
7981
href: core-libraries/6.0/filestream-doesnt-sync-offset-with-os.md
8082
- name: FileStream.Position updated after completion
@@ -609,6 +611,8 @@ items:
609611
href: core-libraries/6.0/debug-assert-conditional-evaluation.md
610612
- name: Environment.ProcessorCount behavior on Windows
611613
href: core-libraries/6.0/environment-processorcount-on-windows.md
614+
- name: File.Replace on Unix throws exceptions to match Windows
615+
href: core-libraries/6.0/file-replace-exceptions-on-unix.md
612616
- name: FileStream no longer synchronizes offset with OS
613617
href: core-libraries/6.0/filestream-doesnt-sync-offset-with-os.md
614618
- name: FileStream.Position updated after completion

0 commit comments

Comments
 (0)