Method MoveToExclusive
MoveToExclusive(IAbsoluteFilePath, bool)
Moves the file to a new location on the same volume in such a way that, of several attempts to move the same file at the same time, at most one succeeds.
Declaration
void MoveToExclusive(IAbsoluteFilePath destinationFile, bool overwrite = false)
Parameters
| Type | Name | Description |
|---|---|---|
| IAbsoluteFilePath | destinationFile | The new location for the file, which must be on the same volume as the file. |
| bool | overwrite | true to allow an existing file to be overwritten; otherwise, false. |
Remarks
MoveTo(IAbsoluteFilePath, bool) does not decide between parties that move the same file to different names at the same time: on Windows each of them reports success and the file ends up under the name that was applied last. With this method one party's move succeeds and every other party's move throws, which makes a move a way to claim a file or to settle which of two changes to it happens. The guarantee is between callers of this method.
A party that loses gets a FileNotFoundException once the file has been moved. On Windows a party whose attempt overlaps the winner's gets an IOException for a sharing violation instead. Any other handle that is open on the file without sharing deletion causes the same sharing violation there, so that exception alone does not say that the file was moved: a caller that tries again has lost when the attempt throws FileNotFoundException.
The file is never copied, so unlike MoveTo(IAbsoluteFilePath, bool) this method throws an IOException if destinationFile is
on another volume. A symbolic link is moved itself, not its target.
On Windows the file is opened with delete access and without sharing deletion, and is renamed through that handle. Handles that are open on the file and share deletion (Delete), such as those of readers and of a writer appending to it, do not block the move and stay valid.
On Unix the file is renamed with a single system call, and open handles never block the move. If overwrite is false, an existing destination is refused as part of the rename on Linux (renameat2 with RENAME_NOREPLACE). On other
Unix platforms, and on file systems that do not support that (NFS, for example), the destination is checked before the rename instead, so a file
that is created there between the two is replaced.
Exceptions
| Type | Condition |
|---|---|
| FileNotFoundException | The file does not exist, which includes that another party has moved it. |
| DirectoryNotFoundException | The file's parent directory or the parent directory of |
| UnauthorizedIOAccessException | Access to the file or to |
| IOException | The file is being moved by another party or is open without sharing deletion (Windows), |