/// <summary> /// Returns the directory information for the specified path string. /// </summary> /// <param name="path">The path of a file or directory.</param> /// <returns>Directory information for path, or <see langword="null"/>, if path denotes a root directory or is <see langword="null"/>.</returns> public static string GetDirectoryName(string path) { // Get the full path without the long path prefix. path = LongPathCommon.RemoveLongPathPrefix(LongPathCommon.NormalizePath(path)); int lastSeparatorIndex = path.LastIndexOfAny(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }); if (lastSeparatorIndex == -1) { return(null); } // Return null for root directories. if (path.Length == 3 && path.EndsWith(@":\", StringComparison.OrdinalIgnoreCase)) { return(null); } if (path.StartsWith(@"\\", StringComparison.OrdinalIgnoreCase) && lastSeparatorIndex <= 2) { return(null); } string result = path.Substring(0, lastSeparatorIndex); // Append directory separator for root directories. if (result.EndsWith(":", StringComparison.OrdinalIgnoreCase)) { return(result + Path.DirectorySeparatorChar); } return(result); }
/// <summary> /// Converts the specified <paramref name="hr"/> to a corresponding the managed exception type. /// </summary> /// <param name="hr">Error code.</param> /// <param name="path">Path to a file or directory.</param> /// <returns>Managed exception for the <paramref name="hr"/> given.</returns> internal static Exception GetExceptionForHr(int hr, string path) { string pathWithoutPrefix = LongPathCommon.RemoveLongPathPrefix(path); Exception nativeException = Marshal.GetExceptionForHR(hr); switch (hr) { case NativeMethods.ErrorFileNotFound: return(new FileNotFoundException(string.Format(CultureInfo.InvariantCulture, "File not found: {0}", pathWithoutPrefix), nativeException)); case NativeMethods.ErrorPathNotFound: return(new DirectoryNotFoundException(string.Format(CultureInfo.InvariantCulture, "Path not found: {0}", pathWithoutPrefix), nativeException)); case NativeMethods.ErrorAccessDenied: return(new UnauthorizedAccessException(string.Format(CultureInfo.InvariantCulture, "Access denied to the path: {0}", pathWithoutPrefix), nativeException)); case NativeMethods.ErrorFilenameExcedRange: return(new PathTooLongException(string.Format(CultureInfo.InvariantCulture, "Path is too long: {0}", pathWithoutPrefix), nativeException)); case NativeMethods.ErrorInvalidDrive: return(new DriveNotFoundException(string.Format(CultureInfo.InvariantCulture, "Drive not found: {0}", pathWithoutPrefix), nativeException)); case NativeMethods.ErrorOperationAborted: return(new OperationCanceledException(string.Format(CultureInfo.InvariantCulture, "Operation aborted: {0}", pathWithoutPrefix), nativeException)); case NativeMethods.ErrorInvalidName: return(new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid name: {0}", pathWithoutPrefix), nativeException)); case NativeMethods.ErrorDirNotEmpty: return(new IOException(string.Format(CultureInfo.InvariantCulture, "Directory is not empty: {0}", pathWithoutPrefix), hr)); case NativeMethods.ErrorFileExists: return(new IOException(string.Format(CultureInfo.InvariantCulture, "File already exists: {0}", pathWithoutPrefix), hr)); } // If we don't know about the error code, rely on the Marshal results. return(nativeException); }
/// <summary> /// Updates the current class' properties. /// </summary> private void UpdateProperties() { this.DirectoryName = LongPathCommon.RemoveLongPathPrefix(LongPathCommon.GetDirectoryName(this.NormalizedPath)); }