Exemple #1
0
        // 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 (string.IsNullOrWhiteSpace(path))
            {
                if (path == null)
                {
                    return(null);
                }
                throw new ArgumentException(SR.Arg_PathEmpty, nameof(path));
            }

            PathInternal.CheckInvalidPathChars(path);
            path = PathInternal.NormalizeDirectorySeparators(path);
            int root = PathInternal.GetRootLength(path);

            int i = path.Length;

            if (i > root)
            {
                while (i > root && !PathInternal.IsDirectorySeparator(path[--i]))
                {
                    ;
                }
                return(path.Substring(0, i));
            }

            return(null);
        }
Exemple #2
0
        // 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)
            {
                PathInternal.CheckInvalidPathChars(path);
                path = PathInternal.NormalizeDirectorySeparators(path);
                int root = PathInternal.GetRootLength(path);

                int i = path.Length;
                if (i > root)
                {
                    i = path.Length;
                    if (i == root)
                    {
                        return(null);
                    }
                    while (i > root && !PathInternal.IsDirectorySeparator(path[--i]))
                    {
                        ;
                    }
                    return(path.Substring(0, i));
                }
            }
            return(null);
        }
Exemple #3
0
        // 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 root = PathInternal.GetRootLength(path);

            int i = path.Length;

            if (i > root)
            {
                while (i > root && !PathInternal.IsDirectorySeparator(path[--i]))
                {
                    ;
                }
                return(path.Substring(0, i));
            }

            return(null);
        }
        public static string GetDirectoryName(string path)
        {
            if (path == null || PathInternal.IsEffectivelyEmpty(path.AsSpan()))
            {
                return((string)null);
            }
            int directoryNameOffset = Path.GetDirectoryNameOffset(path.AsSpan());

            return(directoryNameOffset < 0 ? (string)null : PathInternal.NormalizeDirectorySeparators(path.Substring(0, directoryNameOffset)));
        }
Exemple #5
0
        /// <summary>
        /// Returns the directory portion of a file path. This method effectively
        /// removes the last segment 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
        /// specified path is null, empty, or a root (such as "\", "C:", or
        /// "\\server\share").
        /// </summary>
        /// <remarks>
        /// Directory separators are normalized in the returned string.
        /// </remarks>
        public static string?GetDirectoryName(string?path)
        {
            if (path == null || PathInternal.IsEffectivelyEmpty(path.AsSpan()))
            {
                return(null);
            }

            int end = GetDirectoryNameOffset(path.AsSpan());

            return(end >= 0 ? PathInternal.NormalizeDirectorySeparators(path.Substring(0, end)) : null);
        }
Exemple #6
0
        // Returns the root portion of the given path. The resulting string
        // consists of those rightmost characters of the path that constitute the
        // root of the path. Possible patterns for the resulting string are: An
        // empty string (a relative path on the current drive), "\" (an absolute
        // path on the current drive), "X:" (a relative path on a given drive,
        // where X is the drive letter), "X:\" (an absolute path on a given drive),
        // and "\\server\share" (a UNC path for a given server and share name).
        // The resulting string is null if path is null.
        public static string GetPathRoot(string path)
        {
            if (path == null)
            {
                return(null);
            }
            PathInternal.CheckInvalidPathChars(path);

            // Need to return the normalized directory separator
            path = PathInternal.NormalizeDirectorySeparators(path);

            int pathRoot = PathInternal.GetRootLength(path);

            return(pathRoot <= 0 ? string.Empty : path.Substring(0, pathRoot));
        }
Exemple #7
0
        // Returns the root portion of the given path. The resulting string
        // consists of those rightmost characters of the path that constitute the
        // root of the path. Possible patterns for the resulting string are: An
        // empty string (a relative path on the current drive), "\" (an absolute
        // path on the current drive), "X:" (a relative path on a given drive,
        // where X is the drive letter), "X:\" (an absolute path on a given drive),
        // and "\\server\share" (a UNC path for a given server and share name).
        // The resulting string is null if path is null. If the path is empty or
        // only contains whitespace characters an ArgumentException gets thrown.
        public static string GetPathRoot(string path)
        {
            if (PathInternal.IsEffectivelyEmpty(path))
            {
                return(null);
            }

            ReadOnlySpan <char> result = GetPathRoot(path.AsSpan());

            if (path.Length == result.Length)
            {
                return(PathInternal.NormalizeDirectorySeparators(path));
            }

            return(PathInternal.NormalizeDirectorySeparators(new string(result)));
        }
Exemple #8
0
        // 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);
        }
Exemple #9
0
        // Returns the root portion of the given path. The resulting string
        // consists of those rightmost characters of the path that constitute the
        // root of the path. Possible patterns for the resulting string are: An
        // empty string (a relative path on the current drive), "\" (an absolute
        // path on the current drive), "X:" (a relative path on a given drive,
        // where X is the drive letter), "X:\" (an absolute path on a given drive),
        // and "\\server\share" (a UNC path for a given server and share name).
        // The resulting string is null if path is null. If the path is empty or
        // only contains whitespace characters an ArgumentException gets thrown.
        public static string GetPathRoot(string path)
        {
            if (path == null)
            {
                return(null);
            }
            if (PathInternal.IsEffectivelyEmpty(path))
            {
                throw new ArgumentException(SR.Arg_PathEmpty, nameof(path));
            }

            // Need to return the normalized directory separator
            path = PathInternal.NormalizeDirectorySeparators(path);

            int pathRoot = PathInternal.GetRootLength(path);

            return(pathRoot <= 0 ? string.Empty : path.Substring(0, pathRoot));
        }
Exemple #10
0
        // Returns the root portion of the given path. The resulting string
        // consists of those rightmost characters of the path that constitute the
        // root of the path. Possible patterns for the resulting string are: An
        // empty string (a relative path on the current drive), "\" (an absolute
        // path on the current drive), "X:" (a relative path on a given drive,
        // where X is the drive letter), "X:\" (an absolute path on a given drive),
        // and "\\server\share" (a UNC path for a given server and share name).
        // The resulting string is null if path is null. If the path is empty or
        // only contains whitespace characters an ArgumentException gets thrown.
        public static string GetPathRoot(string path)
        {
            if (path == null)
            {
                return(null);
            }
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentException(SR.Arg_PathEmpty, nameof(path));
            }

            PathInternal.CheckInvalidPathChars(path);

            // Need to return the normalized directory separator
            path = PathInternal.NormalizeDirectorySeparators(path);

            int pathRoot = PathInternal.GetRootLength(path);

            return(pathRoot <= 0 ? string.Empty : path.Substring(0, pathRoot));
        }
Exemple #11
0
        // Returns the root portion of the given path. The resulting string
        // consists of those rightmost characters of the path that constitute the
        // root of the path. Possible patterns for the resulting string are: An
        // empty string (a relative path on the current drive), "\" (an absolute
        // path on the current drive), "X:" (a relative path on a given drive,
        // where X is the drive letter), "X:\" (an absolute path on a given drive),
        // and "\\server\share" (a UNC path for a given server and share name).
        // The resulting string is null if path is null. If the path is empty or
        // only contains whitespace characters an ArgumentException gets thrown.
        public static string GetPathRoot(string path)
        {
            if (path == null)
            {
                return(null);
            }

            if (PathInternal.IsEffectivelyEmpty(path))
            {
                throw new ArgumentException(SR.Arg_PathEmpty, nameof(path));
            }

            ReadOnlySpan <char> result = GetPathRoot(path.AsReadOnlySpan());

            if (path.Length == result.Length)
            {
                return(PathInternal.NormalizeDirectorySeparators(path));
            }

            return(PathInternal.NormalizeDirectorySeparators(new string(result)));
        }