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
pathsstring[]The path segments to combine
Returns
- string
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
pathstringThe 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
GetFileDirectory(string, string)
Gets the directory name from a combined base directory and relative file path.
public static string GetFileDirectory(string baseDirectory, string relFile)
Parameters
Returns
- string
The directory name of the combined path with forward slashes as directory separators
GetParentDirectory(string)
Gets the parent directory of a path, ignoring any trailing slashes.
public static string GetParentDirectory(this string path)
Parameters
pathstringThe 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 replacingPath, string replacedPathKeyword, string replacedNameKeyword)
Parameters
templatePathstringThe template path containing keywords to be replaced
replacingPathstringThe path to use as replacement
replacedPathKeywordstringThe path keyword to be replaced in the template
replacedNameKeywordstringThe name keyword to be replaced in the template
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
relativeTostringThe path that is the reference point
pathstringThe path to which the relative path is calculated
Returns
- string
The relative path with forward slashes as directory separators
GetRelativePathIfDescendant(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 GetRelativePathIfDescendant(string relativeTo, string path)
Parameters
relativeTostringThe path that is the reference point
pathstringThe 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.
GetResourceDirectory(string, string, string)
Combines a base directory with a relative file path and appends a suffix
to derive a sub-directory path. The suffix is only appended when
relFile is not null or whitespace.
public static string GetResourceDirectory(string baseDirectory, string relFile, string suffixIfAdded = "-src")
Parameters
baseDirectorystringThe base directory.
relFilestringThe relative file path.
suffixIfAddedstringThe suffix appended to the combined path when
relFileis not null or whitespace.
Returns
- string
The combined sub-directory path with forward slashes as directory separators.
IsDescendant(DirectoryInfo, string)
Determines whether a path is a descendant of a specified directory.
public static bool IsDescendant(this DirectoryInfo ascendentDirectory, string descendantPath)
Parameters
ascendentDirectoryDirectoryInfoThe potential ancestor directory
descendantPathstringThe 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
ascendentDirectorystringThe potential ancestor directory path
descendantPathstringThe path to check
Returns
- bool
True if the path is a descendant of the directory; otherwise, false
NormalizeToForwardSlash(string)
Replaces backslashes with forward slashes in a path string.
public static string NormalizeToForwardSlash(this string src)
Parameters
srcstringThe source path string
Returns
- string
The path string with all backslashes replaced by forward slashes
ResolveSubDirectory(string, string, string)
Resolves the sub-directory for loading with legacy fallback. Returns the GetResourceDirectory(string, string, string) path if it exists; otherwise falls back to the legacy GetFileDirectory(string, string) path if it exists; otherwise defaults to the GetResourceDirectory(string, string, string) path.
public static string ResolveSubDirectory(string baseDirectory, string relFile, string suffix = "-src")
Parameters
baseDirectorystringThe base directory.
relFilestringThe relative file path.
suffixstringThe suffix for the new sub-directory path.
Returns
- string
The resolved sub-directory path with forward slashes as directory separators.
TrimTrailingSlash(string)
Removes the trailing slash or backslash from a path if it exists.
public static string TrimTrailingSlash(this string path)
Parameters
pathstringThe path to process
Returns
- string
The path without a trailing slash or backslash