Exemple #1
0
        private static IEnumerable <string> EnumerateFileSystemIteratorRecursive(string normalizedPath, string normalizedSearchPattern, bool includeDirectories, bool includeFiles)
        {
            // NOTE: Any exceptions thrown from this method are thrown on a call to IEnumerator<string>.MoveNext()
            var pendingDirectories = new Queue <string>();

            pendingDirectories.Enqueue(normalizedPath);
            while (pendingDirectories.Count > 0)
            {
                normalizedPath = pendingDirectories.Dequeue();
                // get all subdirs to recurse in the next iteration
                foreach (var subdir in EnumerateNormalizedFileSystemEntries(true, false, SearchOption.TopDirectoryOnly, normalizedPath, "*"))
                {
                    pendingDirectories.Enqueue(Path.NormalizeLongPath(subdir));
                }

                var path = Common.IsPathUnc(normalizedPath) ? normalizedPath : Path.RemoveLongPathPrefix(normalizedPath);

                NativeMethods.WIN32_FIND_DATA findData;
                using (var handle = BeginFind(Path.Combine(normalizedPath, normalizedSearchPattern), out findData))
                {
                    if (handle == null)
                    {
                        continue;
                    }

                    do
                    {
                        var fullPath = Path.Combine(path, findData.cFileName);
                        if (IsDirectory(findData.dwFileAttributes))
                        {
                            if (IsCurrentOrParentDirectory(findData.cFileName))
                            {
                                continue;
                            }
                            var fullNormalizedPath = Path.Combine(normalizedPath, findData.cFileName);
                            System.Diagnostics.Debug.Assert(Exists(fullPath));
                            System.Diagnostics.Debug.Assert(Exists(Common.IsPathUnc(fullNormalizedPath) ? fullNormalizedPath : Path.RemoveLongPathPrefix(fullNormalizedPath)));

                            if (includeDirectories)
                            {
                                yield return(Path.RemoveLongPathPrefix(fullPath));
                            }
                        }
                        else if (includeFiles)
                        {
                            yield return(Path.RemoveLongPathPrefix(fullPath));
                        }
                    } while (NativeMethods.FindNextFile(handle, out findData));

                    var errorCode = Marshal.GetLastWin32Error();
                    if (errorCode != NativeMethods.ERROR_NO_MORE_FILES)
                    {
                        throw Common.GetExceptionFromWin32Error(errorCode);
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        ///     Creates the specified directory.
        /// </summary>
        /// <param name="path">
        ///     A <see cref="string"/> containing the path of the directory to create.
        /// </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="System.IO.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="System.IO.DirectoryNotFoundException">
        ///     <paramref name="path"/> contains one or more directories that could not be
        ///     found.
        /// </exception>
        /// <exception cref="UnauthorizedAccessException">
        ///     The caller does not have the required access permissions.
        /// </exception>
        /// <exception cref="System.IO.IOException">
        ///     <paramref name="path"/> is a file.
        ///     <para>
        ///         -or-
        ///     </para>
        ///     <paramref name="path"/> specifies a device that is not ready.
        /// </exception>
        /// <remarks>
        ///     Note: Unlike <see cref="Directory.CreateDirectory(System.String)"/>, this method only creates
        ///     the last directory in <paramref name="path"/>.
        /// </remarks>
        public static DirectoryInfo CreateDirectory(string path)
        {
            if (Common.IsPathUnc(path))
            {
                return(CreateDirectoryUnc(path));
            }
            var normalizedPath = Path.NormalizeLongPath(path);
            var fullPath       = Path.RemoveLongPathPrefix(normalizedPath);

            var length = fullPath.Length;

            if (length >= 2 && Path.IsDirectorySeparator(fullPath[length - 1]))
            {
                --length;
            }

            var rootLength = Path.GetRootLength(fullPath);

            var pathComponents = new List <string>();

            if (length > rootLength)
            {
                for (var index = length - 1; index >= rootLength; --index)
                {
                    var subPath = fullPath.Substring(0, index + 1);
                    if (!Exists(subPath))
                    {
                        pathComponents.Add(Path.NormalizeLongPath(subPath));
                    }
                    while (index > rootLength && fullPath[index] != System.IO.Path.DirectorySeparatorChar &&
                           fullPath[index] != System.IO.Path.AltDirectorySeparatorChar)
                    {
                        --index;
                    }
                }
            }
            while (pathComponents.Count > 0)
            {
                var str = pathComponents[pathComponents.Count - 1];
                pathComponents.RemoveAt(pathComponents.Count - 1);

                if (NativeMethods.CreateDirectory(str, IntPtr.Zero))
                {
                    continue;
                }

                // To mimic Directory.CreateDirectory, we don't throw if the directory (not a file) already exists
                var errorCode = Marshal.GetLastWin32Error();
                // PR: Not sure this is even possible, we check for existance above.
                //if (errorCode != NativeMethods.ERROR_ALREADY_EXISTS || !Exists(path))
                //{
                throw Common.GetExceptionFromWin32Error(errorCode);
                //}
            }
            return(new DirectoryInfo(fullPath));
        }
Exemple #3
0
 public static string GetPathRoot(string path)
 {
     if (path == null)
     {
         return(null);
     }
     if (Path.IsPathRooted(path))
     {
         if (!Common.IsPathUnc(path))
         {
             path = Path.RemoveLongPathPrefix(Path.NormalizeLongPath(path));
         }
         return(path.Substring(0, GetRootLength(path)));
     }
     return(string.Empty);
 }
Exemple #4
0
        private static IEnumerable <string> EnumerateFileSystemIterator(string normalizedPath, string normalizedSearchPattern, bool includeDirectories, bool includeFiles)
        {
            // NOTE: Any exceptions thrown from this method are thrown on a call to IEnumerator<string>.MoveNext()

            var path = Common.IsPathUnc(normalizedPath) ? normalizedPath : Path.RemoveLongPathPrefix(normalizedPath);

            NativeMethods.WIN32_FIND_DATA findData;
            using (var handle = BeginFind(Path.Combine(normalizedPath, normalizedSearchPattern), out findData))
            {
                if (handle == null)
                {
                    yield break;
                }

                do
                {
                    if (IsDirectory(findData.dwFileAttributes))
                    {
                        if (IsCurrentOrParentDirectory(findData.cFileName))
                        {
                            continue;
                        }

                        if (includeDirectories)
                        {
                            yield return(Path.Combine(Path.RemoveLongPathPrefix(path), findData.cFileName));
                        }
                    }
                    else
                    {
                        if (includeFiles)
                        {
                            yield return(Path.Combine(Path.RemoveLongPathPrefix(path), findData.cFileName));
                        }
                    }
                } while (NativeMethods.FindNextFile(handle, out findData));

                var errorCode = Marshal.GetLastWin32Error();
                if (errorCode != NativeMethods.ERROR_NO_MORE_FILES)
                {
                    throw Common.GetExceptionFromWin32Error(errorCode);
                }
            }
        }
Exemple #5
0
        /// <include path='doc/members/member[@name="M:System.IO.Path.GetDirectoryName(System.String)"]/*' file='..\ref\mscorlib.xml' />
        public static string GetDirectoryName(string path)
        {
            if (Common.IsRunningOnMono() && Common.IsPlatformUnix())
            {
                return(System.IO.Path.GetDirectoryName(path));
            }

            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            Path.CheckInvalidPathChars(path);
            string basePath = null;

            if (!IsPathRooted(path))
            {
                basePath = System.IO.Directory.GetCurrentDirectory();
            }

            path = Path.RemoveLongPathPrefix(Path.NormalizeLongPath(path));
            int rootLength = GetRootLength(path);

            if (path.Length <= rootLength)
            {
                return(null);
            }
            int length = path.Length;

            do
            {
            } while (length > rootLength && path[--length] != System.IO.Path.DirectorySeparatorChar &&
                     path[length] != System.IO.Path.AltDirectorySeparatorChar);
            if (basePath != null)
            {
                path   = path.Substring(basePath.Length + 1);
                length = length - basePath.Length - 1;
                if (length < 0)
                {
                    length = 0;
                }
            }
            return(path.Substring(0, length));
        }
Exemple #6
0
 public static string GetFullPath(string path)
 {
     return(Common.IsPathUnc(path) ? path : Path.RemoveLongPathPrefix(Path.NormalizeLongPath(path)));
 }
Exemple #7
0
 public static string GetCurrentDirectory()
 {
     return(Path.RemoveLongPathPrefix(Path.NormalizeLongPath(".")));
 }