Unity-Technologies/UnityCsReference · error · IOException

Failed to Move File / Directory from '{0}' to '{1}'.

Error message

Failed to Move File / Directory from '{0}' to '{1}'.

What it means

After the exists-check, MoveFileOrDirectory calls the native MoveFileOrDirectoryInternal; a false return throws System.IO.IOException with 'Failed to Move File / Directory'. Unlike the copy exists-check, this message is correct. False returns map to OS-level move failures: cross-volume moves when the OS cannot fall back to copy+delete, locked source files, permissions, or missing source.

Source

Thrown at Editor/Mono/FileUtil.bindings.cs:89

        }

        [FreeFunction("CopyFileOrDirectoryFollowSymlinks")]
        private static extern bool CopyFileOrDirectoryFollowSymlinksInternal(string source, string dest);

        // Moves a file or a directory from a given path to another path.
        public static void MoveFileOrDirectory(string source, string dest)
        {
            CheckForValidSourceAndDestinationArgumentsAndRaiseAnExceptionWhenNullOrEmpty(source, dest);

            if (PathExists(dest))
            {
                throw new System.IO.IOException(string.Format(
                    "Failed to Copy File / Directory from '{0}' to '{1}': destination path already exists.", source, dest));
            }

            if (!MoveFileOrDirectoryInternal(source, dest))
            {
                throw new System.IO.IOException(string.Format(
                    "Failed to Move File / Directory from '{0}' to '{1}'.", source, dest));
            }
        }

        [FreeFunction("MoveFileOrDirectory")]
        private static extern bool MoveFileOrDirectoryInternal(string source, string dest);

        private static void CheckForValidSourceAndDestinationArgumentsAndRaiseAnExceptionWhenNullOrEmpty(string source, string dest)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (dest == null) throw new ArgumentNullException("dest");

            if (source == string.Empty) throw new ArgumentException("source", "The source path cannot be empty.");
            if (dest == string.Empty) throw new ArgumentException("dest", "The destination path cannot be empty.");
        }

        // Returns a unique path in the Temp folder within your current project.
        [FreeFunction]

View on GitHub (pinned to 225b0fbdb5)

Solutions

  1. If moving across volumes, fall back to copy-then-delete (CopyFileOrDirectory then DeleteFileOrDirectory) when the native move fails.
  2. Ensure the source is not locked (close the editor/other processes; disable AV scanning of the path).
  3. Validate source existence and that destination is on the same volume for atomic moves.
  4. Catch IOException and surface the OS error code for diagnosis.

Example fix

// before
FileUtil.MoveFileOrDirectory(src, dst);
// after
try { FileUtil.MoveFileOrDirectory(src, dst); }
catch (System.IO.IOException) {
  FileUtil.CopyFileOrDirectory(src, dst);
  FileUtil.DeleteFileOrDirectory(src);
}
Defensive patterns

Strategy: fallback

Validate before calling

if (!FileUtil.PathExists(src)) throw new FileNotFoundException(src);
FileUtil.MoveFileOrDirectory(src, dst);

Try / catch

try { FileUtil.MoveFileOrDirectory(src, dst); }
catch (System.IO.IOException) {
  // cross-volume or locked: fall back to copy + delete
  FileUtil.CopyFileOrDirectory(src, dst);
  FileUtil.DeleteFileOrDirectory(src);
}

Prevention

When it happens

Trigger: Moving across volumes/devices when the underlying OS move cannot do an atomic rename (and Unity does not auto-fallback). Source file is open/locked by Unity or another process. Source path does not exist. Permissions deny the move.

Common situations: Moving Library/ artefacts while the editor holds them open. Moving project files between a system drive and an external/network volume. Antivirus locking newly created files. Moving a folder into one of its own descendants.

Related errors


AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13). Data as JSON: /api/errors/09bc321237ac0681. Report an issue: GitHub.