Пример #1
0
        /// <summary>
        ///     Moves the specified file to a new location.
        /// </summary>
        /// <param name="sourcePath">
        ///     A <see cref="String"/> containing the path of the file to move.
        /// </param>
        /// <param name="destinationPath">
        ///     A <see cref="String"/> containing the new path of the file.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="sourcePath"/> and/or <paramref name="destinationPath"/> is
        ///     <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     <paramref name="sourcePath"/> and/or <paramref name="destinationPath"/> is
        ///     an empty string (""), contains only white space, or contains one or more
        ///     invalid characters as defined in <see cref="Path.GetInvalidPathChars()"/>.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="sourcePath"/> and/or <paramref name="destinationPath"/>
        ///     contains one or more components that exceed the drive-defined maximum length.
        ///     For example, on Windows-based platforms, components must not exceed 255 characters.
        /// </exception>
        /// <exception cref="PathTooLongException">
        ///     <paramref name="sourcePath"/> and/or <paramref name="destinationPath"/>
        ///     exceeds the system-defined maximum length. For example, on Windows-based platforms,
        ///     paths must not exceed 32,000 characters.
        /// </exception>
        /// <exception cref="FileNotFoundException">
        ///     <paramref name="sourcePath"/> could not be found.
        /// </exception>
        /// <exception cref="DirectoryNotFoundException">
        ///     One or more directories in <paramref name="sourcePath"/> and/or
        ///     <paramref name="destinationPath"/> could not be found.
        /// </exception>
        /// <exception cref="UnauthorizedAccessException">
        ///     The caller does not have the required access permissions.
        /// </exception>
        /// <exception cref="IOException">
        ///     <paramref name="destinationPath"/> refers to a file that already exists.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="sourcePath"/> and/or <paramref name="destinationPath"/> is a
        ///     directory.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="sourcePath"/> refers to a file that is in use.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="sourcePath"/> and/or <paramref name="destinationPath"/> specifies
        ///     a device that is not ready.
        /// </exception>
        public static void Move(string sourcePath, string destinationPath)
        {
            if (Common.IsRunningOnMono() && Common.IsPlatformUnix())
            {
                SysFile.Move(sourcePath, destinationPath);
                return;
            }

            string normalizedSourcePath      = Path.NormalizeLongPath(sourcePath, "sourcePath");
            string normalizedDestinationPath = Path.NormalizeLongPath(destinationPath, "destinationPath");

            if (!NativeMethods.MoveFile(normalizedSourcePath, normalizedDestinationPath))
            {
                throw Common.GetExceptionFromLastWin32Error();
            }
        }