Class PathUtil
Utility class for path manipulation and management.
public static class PathUtil
- Inheritance
-
PathUtil
- Inherited Members
Fields
DotAlterWord
Constant used to replace dots in HTTP string parameters.
public const string DotAlterWord = "-(dot)-"
Field Value
Methods
Combine(string, string)
Combines a base path with a subpath, ensuring consistent directory separators.
public static string Combine(string basePath, string subPath)
Parameters
Returns
- string
The combined path with forward slashes as directory separators, or null if basePath is null
Combine(params string[])
Combines multiple path segments, ensuring consistent directory separators.
public static string Combine(params string[] paths)
Parameters
paths
string[]The path segments to combine
Returns
- string
The combined path with forward slashes as directory separators
CombineWithFolderExtension(string, string)
Combines a base directory with a relative file path, preserving the folder structure.
public static string CombineWithFolderExtension(string baseDirectory, string relFile)
Parameters
Returns
- string
The combined path with forward slashes as directory separators
CombineWithoutExtension(params string[])
Combines multiple path segments and removes the file extension from the result.
public static string CombineWithoutExtension(params string[] paths)
Parameters
paths
string[]The path segments to combine
Returns
- string
The combined path without file extension, using forward slashes as directory separators
GetCombinedDirectoryName(string, string)
Gets the directory name from a combined base directory and relative file path.
public static string GetCombinedDirectoryName(string baseDirectory, string relFile)
Parameters
Returns
- string
The directory name of the combined path with forward slashes as directory separators
GetDirectoryName(string)
Gets the directory name of a path, ensuring consistent directory separators.
public static string GetDirectoryName(string path)
Parameters
path
stringThe path to get the directory name from
Returns
- string
The directory name with forward slashes as directory separators, or null if the path has no directory component
GetParentIgnoreTailSlash(string)
Gets the parent directory of a path, ignoring any trailing slashes.
public static string GetParentIgnoreTailSlash(this string path)
Parameters
path
stringThe path to get the parent of
Returns
- string
The parent directory path
GetPathByTemplate(string, string, string, string)
Generates a path by replacing keywords in a template path.
public static string GetPathByTemplate(string templatePath, string replacedPathKeyword, string replacedNameKeyword, string replacingPath)
Parameters
templatePath
stringThe template path containing keywords to be replaced
replacedPathKeyword
stringThe path keyword to be replaced in the template
replacedNameKeyword
stringThe name keyword to be replaced in the template
replacingPath
stringThe path to use as replacement
Returns
- string
The processed path with keywords replaced
GetRelativePath(string, string)
Gets the relative path from one path to another, ensuring consistent directory separators.
public static string GetRelativePath(string relativeTo, string path)
Parameters
relativeTo
stringThe path that is the reference point
path
stringThe path to which the relative path is calculated
Returns
- string
The relative path with forward slashes as directory separators
GetRelativePathIfIsDescendant(string, string)
Gets the relative path if the target path is a descendant of the reference path; otherwise, returns the original path.
public static string GetRelativePathIfIsDescendant(string relativeTo, string path)
Parameters
relativeTo
stringThe path that is the reference point
path
stringThe path to which the relative path is calculated
Returns
- string
The relative path if path is a descendant of relativeTo; otherwise, the original path. Forward slashes are used as directory separators in either case.
IsDescendant(DirectoryInfo, string)
Determines whether a path is a descendant of a specified directory.
public static bool IsDescendant(this DirectoryInfo ascendentDirectory, string descendantPath)
Parameters
ascendentDirectory
DirectoryInfoThe potential ancestor directory
descendantPath
stringThe path to check
Returns
- bool
True if the path is a descendant of the directory; otherwise, false
IsDescendant(string, string)
Determines whether a path is a descendant of a specified directory.
public static bool IsDescendant(string ascendentDirectory, string descendantPath)
Parameters
ascendentDirectory
stringThe potential ancestor directory path
descendantPath
stringThe path to check
Returns
- bool
True if the path is a descendant of the directory; otherwise, false
IsDescendantByRelPath(string, string)
Determines whether a path is a descendant of a specified directory using relative path calculation.
public static bool IsDescendantByRelPath(string ascendentDirectory, string descendantPath)
Parameters
ascendentDirectory
stringThe potential ancestor directory path
descendantPath
stringThe path to check
Returns
- bool
True if the path is a descendant of the directory; otherwise, false
RemoveTailSlashIfExisted(string)
Removes the trailing slash or backslash from a path if it exists.
public static string RemoveTailSlashIfExisted(this string path)
Parameters
path
stringThe path to process
Returns
- string
The path without a trailing slash or backslash
ReplaceDirectorySeparator(string)
Replaces backslashes with forward slashes in a path string.
public static string ReplaceDirectorySeparator(this string src)
Parameters
src
stringThe source path string
Returns
- string
The path string with all backslashes replaced by forward slashes