/// <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 = "System.IO.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" /> 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 = "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 = "System.IO.Directory.CreateDirectory(System.String)" />, this method only creates /// the last directory in <paramref name = "path" />. /// </remarks> public static void Create(Path path) { if (!NativeMethods.CreateDirectory(path.LongFullPath, IntPtr.Zero)) { // To mimic Directory.CreateDirectory, we don't throw if the directory (not a file) already exists var errorCode = Marshal.GetLastWin32Error(); if (errorCode != NativeMethods.ERROR_ALREADY_EXISTS || !Exists(path)) { throw LongPathCommon.GetExceptionFromWin32Error(errorCode); } } }
private static SafeFindHandle BeginFind(string normalizedPathWithSearchPattern, out WIN32_FIND_DATA findData) { var handle = NativeMethods.FindFirstFile(normalizedPathWithSearchPattern, out findData); if (handle.IsInvalid) { var errorCode = Marshal.GetLastWin32Error(); if (errorCode != NativeMethods.ERROR_FILE_NOT_FOUND) { throw LongPathCommon.GetExceptionFromWin32Error(errorCode); } return(null); } return(handle); }
private static IEnumerable <Path> 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 = LongPathCommon.RemoveLongPathPrefix(normalizedPath); WIN32_FIND_DATA findData; using (var handle = BeginFind(System.IO.Path.Combine(normalizedPath, normalizedSearchPattern), out findData)) { if (handle == null) { yield break; } do { var currentFileName = findData.cFileName; if (IsDirectory(findData.dwFileAttributes)) { if (includeDirectories && !IsCurrentOrParentDirectory(currentFileName)) { yield return(path.ToPath().Combine(currentFileName)); } } else { if (includeFiles) { yield return(path.ToPath().Combine(currentFileName)); } } } while (NativeMethods.FindNextFile(handle, out findData)); var errorCode = Marshal.GetLastWin32Error(); if (errorCode != NativeMethods.ERROR_NO_MORE_FILES) { throw LongPathCommon.GetExceptionFromWin32Error(errorCode); } } }
private static IEnumerable <Path> EnumerateFileSystemEntries(Path path, string searchPattern, bool includeDirectories, bool includeFiles) { var normalizedSearchPattern = LongPathCommon.NormalizeSearchPattern(searchPattern); var normalizedPath = LongPathCommon.NormalizeLongPath(path.FullPath); // First check whether the specified path refers to a directory and exists FileAttributes attributes; var errorCode = LongPathCommon.TryGetDirectoryAttributes(normalizedPath, out attributes); if (errorCode != 0) { throw LongPathCommon.GetExceptionFromWin32Error(errorCode); } return(EnumerateFileSystemIterator(normalizedPath, normalizedSearchPattern, includeDirectories, includeFiles)); }