Esempio n. 1
0
        // Normalizes path (can be longer than MAX_PATH) and adds \\?\ long path prefix
        internal static string NormalizeLongPath(string path, string parameterName)
        {
            if (path == null)
            {
                throw new ArgumentNullException(parameterName);
            }

            if (path.Length == 0)
            {
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "'{0}' cannot be an empty string.", parameterName), parameterName);
            }

            StringBuilder buffer = new StringBuilder(path.Length + 1); // Add 1 for NULL
            uint          length = NativeMethods.GetFullPathName(path, (uint)buffer.Capacity, buffer, IntPtr.Zero);

            if (length > buffer.Capacity)
            {
                // Resulting path longer than our buffer, so increase it

                buffer.Capacity = (int)length;
                length          = NativeMethods.GetFullPathName(path, length, buffer, IntPtr.Zero);
            }

            if (length == 0)
            {
                throw LongPathCommon.GetExceptionFromLastWin32Error(parameterName);
            }

            if (length > NativeMethods.MAX_LONG_PATH)
            {
                throw LongPathCommon.GetExceptionFromWin32Error(NativeMethods.ERROR_FILENAME_EXCED_RANGE, parameterName);
            }

            return(AddLongPathPrefix(buffer.ToString()));
        }
Esempio n. 2
0
        /// <summary>
        ///     Deletes the specified empty directory.
        /// </summary>
        /// <param name="path">
        ///      A <see cref="String"/> containing the path of the directory to delete.
        /// </param>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="path"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     <paramref name="path"/> 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="path"/> 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="path"/> exceeds the system-defined maximum length.
        ///     For example, on Windows-based platforms, paths must not exceed
        ///     32,000 characters.
        /// </exception>
        /// <exception cref="DirectoryNotFoundException">
        ///     <paramref name="path"/> could not be found.
        /// </exception>
        /// <exception cref="UnauthorizedAccessException">
        ///     The caller does not have the required access permissions.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="path"/> refers to a directory that is read-only.
        /// </exception>
        /// <exception cref="IOException">
        ///     <paramref name="path"/> is a file.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="path"/> refers to a directory that is not empty.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="path"/> refers to a directory that is in use.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="path"/> specifies a device that is not ready.
        /// </exception>
        public static void Delete(string path)
        {
            string normalizedPath = LongPathCommon.NormalizeLongPath(path);

            if (!NativeMethods.RemoveDirectory(normalizedPath))
            {
                throw LongPathCommon.GetExceptionFromLastWin32Error();
            }
        }
Esempio n. 3
0
        /// <summary>
        ///     Copies the specified file to a specified new file, indicating whether to overwrite an existing file.
        /// </summary>
        /// <param name="sourcePath">
        ///     A <see cref="String"/> containing the path of the file to copy.
        /// </param>
        /// <param name="destinationPath">
        ///     A <see cref="String"/> containing the new path of the file.
        /// </param>
        /// <param name="overwrite">
        ///     <see langword="true"/> if <paramref name="destinationPath"/> should be overwritten
        ///     if it refers to an existing file, otherwise, <see langword="false"/>.
        /// </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.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="overwrite"/> is true and <paramref name="destinationPath"/> refers to a
        ///     file that is read-only.
        /// </exception>
        /// <exception cref="IOException">
        ///     <paramref name="overwrite"/> is false and <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="overwrite"/> is true and <paramref name="destinationPath"/> refers to
        ///     a file that already exists and is in use.
        ///     <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 Copy(string sourcePath, string destinationPath, bool overwrite)
        {
            string normalizedSourcePath      = LongPathCommon.NormalizeLongPath(sourcePath, "sourcePath");
            string normalizedDestinationPath = LongPathCommon.NormalizeLongPath(destinationPath, "destinationPath");

            if (!NativeMethods.CopyFile(normalizedSourcePath, normalizedDestinationPath, !overwrite))
            {
                throw LongPathCommon.GetExceptionFromLastWin32Error();
            }
        }
Esempio n. 4
0
        private static SafeFileHandle GetFileHandle(string normalizedPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
        {
            NativeMethods.EFileAccess underlyingAccess = GetUnderlyingAccess(access);

            SafeFileHandle handle = NativeMethods.CreateFile(normalizedPath, underlyingAccess, (uint)share, IntPtr.Zero, (uint)mode, (uint)options, IntPtr.Zero);

            if (handle.IsInvalid)
            {
                throw LongPathCommon.GetExceptionFromLastWin32Error();
            }

            return(handle);
        }