Table of Contents

Class Path

Namespace
Loehnert.Utility
Assembly
Loehnert.Utility.dll

Performs operations on System.String instances that contain file or directory path information.

public static class Path
Inheritance
Path
Inherited Members

Methods

Combine(params string[])

Gets the combined full path.

public static string Combine(params string[] paths)

Parameters

paths string[]

Paths to combine.

Returns

string

Combined absolute path.

Exceptions

ArgumentNullException

Thrown when paths is null.

GetBaseDirectory(string, string)

Gets the base path that ends with a folder name. This function is case invariant (e.g: GetBaseDirectory("D:\Lisrt\Projects\LisRT\SomeFolder", "LISRT") returns "D:\Lisrt\Projects\LisRT").

public static string GetBaseDirectory(string path, string baseFolder)

Parameters

path string

Path containing the base folder.

baseFolder string

Folder to be searched for.

Returns

string

null when the path does not contain a folder named baseFolder, otherwise the base folder name.

Examples

string basePath = Path.GetBaseDirectory("D:\Lisrt\Projects\LisRT\SomeFolder", "LISRT");
returns "D:\Lisrt\Projects\LisRT"

Exceptions

ArgumentNullException

Thrown when a parameter is null.

ArgumentException

Thrown when baseFolder contains a directory separator char.

GetRelativPath(string, string)

Gets the path, relative to a base directory if possible.

public static string GetRelativPath(string path, string basePath)

Parameters

path string

Absolute path, containing the base directory. This must not be a path to a file.

basePath string

Absolute base directory.

Returns

string

The path relative to the basePath or path if path doesn't contain basePath.

Exceptions

ArgumentNullException

Thrown when path or basePath is null, empty or whitespace.

ArgumentException

Thrown when basePath contains a file extension.

IsSubPathOf(string, string)

Returns true if path starts with the path baseDirPath. The comparison is case-insensitive, handles / and \ slashes as folder separators and only matches if the base directory folder name is matched exactly ("c:\foobar\file.txt" is not a sub path of "c:\foo").

public static bool IsSubPathOf(this string path, string baseDirPath)

Parameters

path string

Path which could contain baseDirPath.

baseDirPath string

Path part.

Returns

bool

True if path starts with the path baseDirPath.

Remarks