/// <summary> /// Returns the directory path of a file path. /// The returned value the an empty ReadOnlySpan if path is empty or /// if the file path denotes as root (such as "\", "C:", or "\\server\share"). /// </summary> /// <remarks> /// Unlike the string overload, this method will not normalize directory separators. /// </remarks> public static ReadOnlySpan <char> GetDirectoryName(ReadOnlySpan <char> path) { if (PathInternal.IsEffectivelyEmpty(path)) { return(ReadOnlySpan <char> .Empty); } int end = PathInternal.GetDirectoryNameOffset(path); return(end >= 0 ? path.Slice(0, end) : ReadOnlySpan <char> .Empty); }
// Returns the directory path of a file path. This method effectively // removes the last element of the given file path, i.e. it returns a // string consisting of all characters up to but not including the last // backslash ("\") in the file path. The returned value is null if the file // path is null or if the file path denotes a root (such as "\", "C:", or // "\\server\share"). public static string GetDirectoryName(string path) { if (path == null) { return(null); } if (PathInternal.IsEffectivelyEmpty(path)) { throw new ArgumentException(SR.Arg_PathEmpty, nameof(path)); } path = PathInternal.NormalizeDirectorySeparators(path); int end = PathInternal.GetDirectoryNameOffset(path); return(end >= 0 ? path.Substring(0, end) : null); }